Skip to content

Commit 1cea9ee

Browse files
committed
add prototype bean ex with function
1 parent 0242d74 commit 1cea9ee

6 files changed

Lines changed: 115 additions & 0 deletions

File tree

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

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

33
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.prototype.PrototypeBeanWithParam;
45
import com.baeldung.scope.singletone.SingletonAppContextBean;
56
import com.baeldung.scope.singletone.SingletonBean;
7+
import com.baeldung.scope.singletone.SingletonFunctionBean;
68
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
79
import com.baeldung.scope.singletone.SingletonProviderBean;
10+
11+
import java.util.function.Function;
12+
813
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
914
import org.springframework.context.annotation.Bean;
1015
import 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
}

‎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
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
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.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+
}

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

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

33
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.prototype.PrototypeBeanWithParam;
5+
import com.baeldung.scope.singletone.SingletonFunctionBean;
46
import com.baeldung.scope.singletone.SingletonLookupBean;
57
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
68
import 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
}

0 commit comments

Comments
 (0)