File tree Expand file tree Collapse file tree
main/java/com/baeldung/scope
test/java/com/baeldung/scope Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import com .baeldung .scope .prototype .PrototypeBean ;
44import com .baeldung .scope .singletone .SingletonAppContextBean ;
55import com .baeldung .scope .singletone .SingletonBean ;
6- import com .baeldung .scope .singletone .SingletonFunctionBean ;
76import com .baeldung .scope .singletone .SingletonObjectFactoryBean ;
87import com .baeldung .scope .singletone .SingletonProviderBean ;
98
10- import java .util .function .Function ;
11-
129import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
1310import org .springframework .context .annotation .Bean ;
1411import org .springframework .context .annotation .ComponentScan ;
@@ -45,19 +42,4 @@ public SingletonObjectFactoryBean singletonObjectFactoryBean() {
4542 return new SingletonObjectFactoryBean ();
4643 }
4744
48- @ Bean
49- public Function <String , PrototypeBean > beanFactory () {
50- return name -> prototypeBeanWithParam (name );
51- }
52-
53- @ Bean
54- @ Scope (value = "prototype" )
55- public PrototypeBean prototypeBeanWithParam (String name ) {
56- return new PrototypeBean (name );
57- }
58-
59- @ Bean
60- public SingletonFunctionBean singletonFunctionBean () {
61- return new SingletonFunctionBean ();
62- }
6345}
Original file line number Diff line number Diff line change 1+ package com .baeldung .scope ;
2+
3+ import java .util .function .Function ;
4+
5+ import org .springframework .beans .factory .annotation .Qualifier ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .context .annotation .Scope ;
9+
10+ import com .baeldung .scope .prototype .PrototypeBean ;
11+ import com .baeldung .scope .singletone .SingletonFunctionBean ;
12+
13+ @ Configuration
14+ public class AppConfigFunctionBean {
15+
16+ @ Bean
17+ public Function <String , PrototypeBean > beanFactory () {
18+ return name -> prototypeBeanWithParam (name );
19+ }
20+
21+ @ Bean
22+ @ Scope (value = "prototype" )
23+ public PrototypeBean prototypeBeanWithParam (String name ) {
24+ return new PrototypeBean (name );
25+ }
26+
27+ @ Bean
28+ public SingletonFunctionBean singletonFunctionBean () {
29+ return new SingletonFunctionBean ();
30+ }
31+
32+ }
Original file line number Diff line number Diff line change @@ -60,19 +60,4 @@ public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() {
6060 Assert .assertTrue ("New instance expected" , firstInstance != secondInstance );
6161 }
6262
63-
64- @ Test
65- public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn () {
66-
67- AbstractApplicationContext context = new AnnotationConfigApplicationContext (AppConfig .class );
68-
69- SingletonFunctionBean firstContext = context .getBean (SingletonFunctionBean .class );
70- SingletonFunctionBean secondContext = context .getBean (SingletonFunctionBean .class );
71-
72- PrototypeBean firstInstance = firstContext .getPrototypeInstance ("instance1" );
73- PrototypeBean secondInstance = secondContext .getPrototypeInstance ("instance2" );
74-
75- Assert .assertTrue ("New instance expected" , firstInstance != secondInstance );
76- }
77-
7863}
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 .factorybean .FactoryBeanAppConfig ;
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 = FactoryBeanAppConfig .class )
18+ public class PrototypeFunctionBeanIntegrationTest {
19+
20+
21+ @ Test
22+ public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn () {
23+
24+ AbstractApplicationContext context = new AnnotationConfigApplicationContext (AppConfig .class );
25+
26+ SingletonFunctionBean firstContext = context .getBean (SingletonFunctionBean .class );
27+ SingletonFunctionBean secondContext = context .getBean (SingletonFunctionBean .class );
28+
29+ PrototypeBean firstInstance = firstContext .getPrototypeInstance ("instance1" );
30+ PrototypeBean secondInstance = secondContext .getPrototypeInstance ("instance2" );
31+
32+ Assert .assertTrue ("New instance expected" , firstInstance != secondInstance );
33+ }
34+
35+ }
You can’t perform that action at this time.
0 commit comments