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 11package com .baeldung .scope ;
22
33import com .baeldung .scope .prototype .PrototypeBean ;
4+ import com .baeldung .scope .prototype .PrototypeBeanWithParam ;
45import com .baeldung .scope .singletone .SingletonAppContextBean ;
56import com .baeldung .scope .singletone .SingletonBean ;
7+ import com .baeldung .scope .singletone .SingletonFunctionBean ;
68import com .baeldung .scope .singletone .SingletonObjectFactoryBean ;
79import com .baeldung .scope .singletone .SingletonProviderBean ;
10+
11+ import java .util .function .Function ;
12+
813import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
914import org .springframework .context .annotation .Bean ;
1015import org .springframework .context .annotation .ComponentScan ;
@@ -40,4 +45,20 @@ public SingletonAppContextBean singletonAppContextBean() {
4045 public SingletonObjectFactoryBean singletonObjectFactoryBean () {
4146 return new SingletonObjectFactoryBean ();
4247 }
48+
49+ @ Bean
50+ public Function <String , PrototypeBeanWithParam > beanFactory () {
51+ return name -> prototypeBeanWithParam (name );
52+ }
53+
54+ @ Bean
55+ @ Scope (value = "prototype" )
56+ public PrototypeBeanWithParam prototypeBeanWithParam (String name ) {
57+ return new PrototypeBeanWithParam (name );
58+ }
59+
60+ @ Bean
61+ public SingletonFunctionBean singletonFunctionBean () {
62+ return new SingletonFunctionBean ();
63+ }
4364}
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 1+ package com .baeldung .scope ;
2+
3+ import java .util .function .Function ;
4+
5+ import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .context .annotation .Scope ;
9+ import org .springframework .context .annotation .ScopedProxyMode ;
10+
11+ import com .baeldung .scope .prototype .PrototypeBeanWithParam ;
12+
13+ @ Configuration
14+ public class PrototypeFactoryBeanConfig {
15+
16+ @ Bean
17+ @ Scope (value = ConfigurableBeanFactory .SCOPE_PROTOTYPE , proxyMode = ScopedProxyMode .TARGET_CLASS )
18+ public PrototypeBeanWithParam prototypeBeanWithParam (String name ) {
19+ return new PrototypeBeanWithParam (name );
20+ }
21+
22+ @ Bean
23+ public Function <String , PrototypeBeanWithParam > prototypeBeanFactory () {
24+ return runtimeArg -> prototypeBeanWithParam (runtimeArg );
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scope .prototype ;
2+
3+ import org .apache .log4j .Logger ;
4+
5+ public class PrototypeBeanWithParam {
6+
7+ private String name ;
8+
9+ private final Logger logger = Logger .getLogger (this .getClass ());
10+
11+ public PrototypeBeanWithParam () {
12+ logger .info ("Prototype instance with param created" );
13+ }
14+
15+ public PrototypeBeanWithParam (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+
28+ }
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 .PrototypeBeanWithParam ;
8+
9+ public class SingletonFunctionBean {
10+
11+ @ Autowired
12+ private Function <String , PrototypeBeanWithParam > beanFactory ;
13+
14+ public PrototypeBeanWithParam getPrototypeInstance (String name ) {
15+ PrototypeBeanWithParam 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 .prototype .PrototypeBeanWithParam ;
5+ import com .baeldung .scope .singletone .SingletonFunctionBean ;
46import com .baeldung .scope .singletone .SingletonLookupBean ;
57import com .baeldung .scope .singletone .SingletonObjectFactoryBean ;
68import com .baeldung .scope .singletone .SingletonProviderBean ;
@@ -58,4 +60,20 @@ public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() {
5860
5961 Assert .assertTrue ("New instance expected" , firstInstance != secondInstance );
6062 }
63+
64+
65+ @ Test
66+ public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn () {
67+
68+ AbstractApplicationContext context = new AnnotationConfigApplicationContext (AppConfig .class );
69+
70+ SingletonFunctionBean firstContext = context .getBean (SingletonFunctionBean .class );
71+ SingletonFunctionBean secondContext = context .getBean (SingletonFunctionBean .class );
72+
73+ PrototypeBeanWithParam firstInstance = firstContext .getPrototypeInstance ("instance1" );
74+ PrototypeBeanWithParam secondInstance = secondContext .getPrototypeInstance ("instance2" );
75+
76+ Assert .assertTrue ("New instance expected" , firstInstance != secondInstance );
77+ }
78+
6179}
You can’t perform that action at this time.
0 commit comments