Skip to content

Commit 32a9212

Browse files
committed
separate configs
1 parent fd34932 commit 32a9212

4 files changed

Lines changed: 67 additions & 33 deletions

File tree

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import com.baeldung.scope.prototype.PrototypeBean;
44
import com.baeldung.scope.singletone.SingletonAppContextBean;
55
import com.baeldung.scope.singletone.SingletonBean;
6-
import com.baeldung.scope.singletone.SingletonFunctionBean;
76
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
87
import com.baeldung.scope.singletone.SingletonProviderBean;
98

10-
import java.util.function.Function;
11-
129
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
1310
import org.springframework.context.annotation.Bean;
1411
import 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
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

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

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

0 commit comments

Comments
 (0)