From 32a9212fc9bbfcfdd65aec485c47d79323f5f109 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 19:22:26 +0300 Subject: [PATCH] separate configs --- .../java/com/baeldung/scope/AppConfig.java | 18 ---------- .../baeldung/scope/AppConfigFunctionBean.java | 32 +++++++++++++++++ ...PrototypeBeanInjectionIntegrationTest.java | 15 -------- .../PrototypeFunctionBeanIntegrationTest.java | 35 +++++++++++++++++++ 4 files changed, 67 insertions(+), 33 deletions(-) create mode 100644 spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java create mode 100644 spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java diff --git a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java index e1395ba72f..18c53fee15 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java @@ -3,12 +3,9 @@ package com.baeldung.scope; import com.baeldung.scope.prototype.PrototypeBean; import com.baeldung.scope.singletone.SingletonAppContextBean; import com.baeldung.scope.singletone.SingletonBean; -import com.baeldung.scope.singletone.SingletonFunctionBean; import com.baeldung.scope.singletone.SingletonObjectFactoryBean; import com.baeldung.scope.singletone.SingletonProviderBean; -import java.util.function.Function; - import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -45,19 +42,4 @@ public class AppConfig { return new SingletonObjectFactoryBean(); } - @Bean - public Function beanFactory() { - return name -> prototypeBeanWithParam(name); - } - - @Bean - @Scope(value = "prototype") - public PrototypeBean prototypeBeanWithParam(String name) { - return new PrototypeBean(name); - } - - @Bean - public SingletonFunctionBean singletonFunctionBean() { - return new SingletonFunctionBean(); - } } diff --git a/spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java b/spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java new file mode 100644 index 0000000000..80779b62fd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java @@ -0,0 +1,32 @@ +package com.baeldung.scope; + +import java.util.function.Function; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +import com.baeldung.scope.prototype.PrototypeBean; +import com.baeldung.scope.singletone.SingletonFunctionBean; + +@Configuration +public class AppConfigFunctionBean { + + @Bean + public Function beanFactory() { + return name -> prototypeBeanWithParam(name); + } + + @Bean + @Scope(value = "prototype") + public PrototypeBean prototypeBeanWithParam(String name) { + return new PrototypeBean(name); + } + + @Bean + public SingletonFunctionBean singletonFunctionBean() { + return new SingletonFunctionBean(); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java b/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java index 68b4e6ff73..d0c2733765 100644 --- a/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java +++ b/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java @@ -60,19 +60,4 @@ public class PrototypeBeanInjectionIntegrationTest { Assert.assertTrue("New instance expected", firstInstance != secondInstance); } - - @Test - public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn() { - - AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); - - SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class); - SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class); - - PrototypeBean firstInstance = firstContext.getPrototypeInstance("instance1"); - PrototypeBean secondInstance = secondContext.getPrototypeInstance("instance2"); - - Assert.assertTrue("New instance expected", firstInstance != secondInstance); - } - } diff --git a/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java b/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java new file mode 100644 index 0000000000..6c2309d86f --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.scope; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.factorybean.FactoryBeanAppConfig; +import com.baeldung.scope.prototype.PrototypeBean; +import com.baeldung.scope.singletone.SingletonFunctionBean; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = FactoryBeanAppConfig.class) +public class PrototypeFunctionBeanIntegrationTest { + + + @Test + public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn() { + + AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); + + SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class); + SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class); + + PrototypeBean firstInstance = firstContext.getPrototypeInstance("instance1"); + PrototypeBean secondInstance = secondContext.getPrototypeInstance("instance2"); + + Assert.assertTrue("New instance expected", firstInstance != secondInstance); + } + +}