File tree Expand file tree Collapse file tree
test/java/com/baeldung/scope Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -40,4 +40,5 @@ public SingletonAppContextBean singletonAppContextBean() {
4040 public SingletonObjectFactoryBean singletonObjectFactoryBean () {
4141 return new SingletonObjectFactoryBean ();
4242 }
43+
4344}
Original file line number Diff line number Diff line change 22
33import com .baeldung .scope .prototype .PrototypeBean ;
44import com .baeldung .scope .singletone .SingletonBean ;
5+
56import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
67import org .springframework .context .annotation .*;
78
@@ -19,4 +20,5 @@ public PrototypeBean prototypeBean() {
1920 public SingletonBean singletonBean () {
2021 return new SingletonBean ();
2122 }
23+
2224}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package com .baeldung .scope ;
22
33import com .baeldung .scope .prototype .PrototypeBean ;
4+ import com .baeldung .scope .singletone .SingletonFunctionBean ;
45import com .baeldung .scope .singletone .SingletonLookupBean ;
56import com .baeldung .scope .singletone .SingletonObjectFactoryBean ;
67import 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments