Skip to content

Commit ca1908a

Browse files
authored
Merge pull request eugenp#4542 from eugenp/prototype-bean-function
add prototype bean ex with function
2 parents 24a52e8 + 679336f commit ca1908a

7 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.config.scope;
2+
3+
import java.util.function.Function;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Scope;
8+
9+
import com.baeldung.scope.prototype.PrototypeBean;
10+
import com.baeldung.scope.singletone.SingletonFunctionBean;
11+
12+
@Configuration
13+
public class AppConfigFunctionBean {
14+
15+
@Bean
16+
public Function<String, PrototypeBean> beanFactory() {
17+
return name -> prototypeBeanWithParam(name);
18+
}
19+
20+
@Bean
21+
@Scope(value = "prototype")
22+
public PrototypeBean prototypeBeanWithParam(String name) {
23+
return new PrototypeBean(name);
24+
}
25+
26+
@Bean
27+
public SingletonFunctionBean singletonFunctionBean() {
28+
return new SingletonFunctionBean();
29+
}
30+
31+
}

‎spring-core/src/main/java/com/baeldung/scope/AppConfig.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ public SingletonAppContextBean singletonAppContextBean() {
4040
public SingletonObjectFactoryBean singletonObjectFactoryBean() {
4141
return new SingletonObjectFactoryBean();
4242
}
43+
4344
}

‎spring-core/src/main/java/com/baeldung/scope/AppProxyScopeConfig.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.baeldung.scope.prototype.PrototypeBean;
44
import com.baeldung.scope.singletone.SingletonBean;
5+
56
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
67
import org.springframework.context.annotation.*;
78

@@ -19,4 +20,5 @@ public PrototypeBean prototypeBean() {
1920
public SingletonBean singletonBean() {
2021
return new SingletonBean();
2122
}
23+
2224
}

‎spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBean.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,20 @@ public class PrototypeBean {
99
public PrototypeBean() {
1010
logger.info("Prototype instance created");
1111
}
12+
13+
private String name;
14+
15+
public PrototypeBean(String name) {
16+
this.name = name;
17+
logger.info("Prototype instance " + name + " created");
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
1228
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.scope.singletone;
2+
3+
import java.util.function.Function;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import com.baeldung.scope.prototype.PrototypeBean;
8+
9+
public class SingletonFunctionBean {
10+
11+
@Autowired
12+
private Function<String, PrototypeBean> beanFactory;
13+
14+
public PrototypeBean getPrototypeInstance(String name) {
15+
PrototypeBean bean = beanFactory.apply(name);
16+
return bean;
17+
}
18+
19+
}

‎spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.scope;
22

33
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.singletone.SingletonFunctionBean;
45
import com.baeldung.scope.singletone.SingletonLookupBean;
56
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
67
import com.baeldung.scope.singletone.SingletonProviderBean;
@@ -58,4 +59,5 @@ public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() {
5859

5960
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
6061
}
62+
6163
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.scope;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7+
import org.springframework.context.support.AbstractApplicationContext;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
11+
12+
import com.baeldung.config.scope.AppConfigFunctionBean;
13+
import com.baeldung.scope.prototype.PrototypeBean;
14+
import com.baeldung.scope.singletone.SingletonFunctionBean;
15+
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfigFunctionBean.class)
18+
public class PrototypeFunctionBeanIntegrationTest {
19+
20+
@Test
21+
public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn() {
22+
23+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfigFunctionBean.class);
24+
25+
SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class);
26+
SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class);
27+
28+
PrototypeBean firstInstance = firstContext.getPrototypeInstance("instance1");
29+
PrototypeBean secondInstance = secondContext.getPrototypeInstance("instance2");
30+
31+
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)