From 1cea9ee0885cc1ff978586a55b7729520cbb2f84 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 13:03:31 +0300 Subject: [PATCH 1/6] add prototype bean ex with function --- .../java/com/baeldung/scope/AppConfig.java | 21 ++++++++++++++ .../baeldung/scope/AppProxyScopeConfig.java | 2 ++ .../scope/PrototypeFactoryBeanConfig.java | 27 ++++++++++++++++++ .../prototype/PrototypeBeanWithParam.java | 28 +++++++++++++++++++ .../singletone/SingletonFunctionBean.java | 19 +++++++++++++ ...PrototypeBeanInjectionIntegrationTest.java | 18 ++++++++++++ 6 files changed, 115 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java create mode 100644 spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.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 5c8c539e7a..6a6efe26c5 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java @@ -1,10 +1,15 @@ package com.baeldung.scope; import com.baeldung.scope.prototype.PrototypeBean; +import com.baeldung.scope.prototype.PrototypeBeanWithParam; 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; @@ -40,4 +45,20 @@ public class AppConfig { public SingletonObjectFactoryBean singletonObjectFactoryBean() { return new SingletonObjectFactoryBean(); } + + @Bean + public Function beanFactory() { + return name -> prototypeBeanWithParam(name); + } + + @Bean + @Scope(value = "prototype") + public PrototypeBeanWithParam prototypeBeanWithParam(String name) { + return new PrototypeBeanWithParam(name); + } + + @Bean + public SingletonFunctionBean singletonFunctionBean() { + return new SingletonFunctionBean(); + } } diff --git a/spring-core/src/main/java/com/baeldung/scope/AppProxyScopeConfig.java b/spring-core/src/main/java/com/baeldung/scope/AppProxyScopeConfig.java index fc7a30471c..9f1874375e 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppProxyScopeConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppProxyScopeConfig.java @@ -2,6 +2,7 @@ package com.baeldung.scope; import com.baeldung.scope.prototype.PrototypeBean; import com.baeldung.scope.singletone.SingletonBean; + import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.*; @@ -19,4 +20,5 @@ public class AppProxyScopeConfig { public SingletonBean singletonBean() { return new SingletonBean(); } + } diff --git a/spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java b/spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java new file mode 100644 index 0000000000..6196672db8 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java @@ -0,0 +1,27 @@ +package com.baeldung.scope; + +import java.util.function.Function; + +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; + +import com.baeldung.scope.prototype.PrototypeBeanWithParam; + +@Configuration +public class PrototypeFactoryBeanConfig { + + @Bean + @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) + public PrototypeBeanWithParam prototypeBeanWithParam(String name) { + return new PrototypeBeanWithParam(name); + } + + @Bean + public Function prototypeBeanFactory() { + return runtimeArg -> prototypeBeanWithParam(runtimeArg); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java b/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java new file mode 100644 index 0000000000..018084db09 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java @@ -0,0 +1,28 @@ +package com.baeldung.scope.prototype; + +import org.apache.log4j.Logger; + +public class PrototypeBeanWithParam { + + private String name; + + private final Logger logger = Logger.getLogger(this.getClass()); + + public PrototypeBeanWithParam() { + logger.info("Prototype instance with param created"); + } + + public PrototypeBeanWithParam(String name) { + this.name = name; + logger.info("Prototype instance " + name + " created"); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java b/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java new file mode 100644 index 0000000000..5cc40549a0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java @@ -0,0 +1,19 @@ +package com.baeldung.scope.singletone; + +import java.util.function.Function; + +import org.springframework.beans.factory.annotation.Autowired; + +import com.baeldung.scope.prototype.PrototypeBeanWithParam; + +public class SingletonFunctionBean { + + @Autowired + private Function beanFactory; + + public PrototypeBeanWithParam getPrototypeInstance(String name) { + PrototypeBeanWithParam bean = beanFactory.apply(name); + return bean; + } + +} 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 1c05bb3e8e..719e25eb51 100644 --- a/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java +++ b/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java @@ -1,6 +1,8 @@ package com.baeldung.scope; import com.baeldung.scope.prototype.PrototypeBean; +import com.baeldung.scope.prototype.PrototypeBeanWithParam; +import com.baeldung.scope.singletone.SingletonFunctionBean; import com.baeldung.scope.singletone.SingletonLookupBean; import com.baeldung.scope.singletone.SingletonObjectFactoryBean; import com.baeldung.scope.singletone.SingletonProviderBean; @@ -58,4 +60,20 @@ 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); + + PrototypeBeanWithParam firstInstance = firstContext.getPrototypeInstance("instance1"); + PrototypeBeanWithParam secondInstance = secondContext.getPrototypeInstance("instance2"); + + Assert.assertTrue("New instance expected", firstInstance != secondInstance); + } + } From c19dfa510a821b34f8471d76dc36e30e21d37a92 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 19:16:02 +0300 Subject: [PATCH 2/6] remove extra classes --- .../java/com/baeldung/scope/AppConfig.java | 6 ++-- .../scope/PrototypeFactoryBeanConfig.java | 27 ------------------ .../scope/prototype/PrototypeBean.java | 16 +++++++++++ .../prototype/PrototypeBeanWithParam.java | 28 ------------------- .../singletone/SingletonFunctionBean.java | 8 +++--- ...PrototypeBeanInjectionIntegrationTest.java | 5 ++-- 6 files changed, 25 insertions(+), 65 deletions(-) delete mode 100644 spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java delete mode 100644 spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.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 6a6efe26c5..ee6ae1a924 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java @@ -47,14 +47,14 @@ public class AppConfig { } @Bean - public Function beanFactory() { + public Function beanFactory() { return name -> prototypeBeanWithParam(name); } @Bean @Scope(value = "prototype") - public PrototypeBeanWithParam prototypeBeanWithParam(String name) { - return new PrototypeBeanWithParam(name); + public PrototypeBean prototypeBeanWithParam(String name) { + return new PrototypeBean(name); } @Bean diff --git a/spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java b/spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java deleted file mode 100644 index 6196672db8..0000000000 --- a/spring-core/src/main/java/com/baeldung/scope/PrototypeFactoryBeanConfig.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.scope; - -import java.util.function.Function; - -import org.springframework.beans.factory.config.ConfigurableBeanFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; -import org.springframework.context.annotation.ScopedProxyMode; - -import com.baeldung.scope.prototype.PrototypeBeanWithParam; - -@Configuration -public class PrototypeFactoryBeanConfig { - - @Bean - @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) - public PrototypeBeanWithParam prototypeBeanWithParam(String name) { - return new PrototypeBeanWithParam(name); - } - - @Bean - public Function prototypeBeanFactory() { - return runtimeArg -> prototypeBeanWithParam(runtimeArg); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBean.java b/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBean.java index cfccb62e45..c87c7a8d44 100644 --- a/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBean.java +++ b/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBean.java @@ -9,4 +9,20 @@ public class PrototypeBean { public PrototypeBean() { logger.info("Prototype instance created"); } + + private String name; + + public PrototypeBean(String name) { + this.name = name; + logger.info("Prototype instance " + name + " created"); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } diff --git a/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java b/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java deleted file mode 100644 index 018084db09..0000000000 --- a/spring-core/src/main/java/com/baeldung/scope/prototype/PrototypeBeanWithParam.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.scope.prototype; - -import org.apache.log4j.Logger; - -public class PrototypeBeanWithParam { - - private String name; - - private final Logger logger = Logger.getLogger(this.getClass()); - - public PrototypeBeanWithParam() { - logger.info("Prototype instance with param created"); - } - - public PrototypeBeanWithParam(String name) { - this.name = name; - logger.info("Prototype instance " + name + " created"); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java b/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java index 5cc40549a0..8cdc56a6fa 100644 --- a/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java +++ b/spring-core/src/main/java/com/baeldung/scope/singletone/SingletonFunctionBean.java @@ -4,15 +4,15 @@ import java.util.function.Function; import org.springframework.beans.factory.annotation.Autowired; -import com.baeldung.scope.prototype.PrototypeBeanWithParam; +import com.baeldung.scope.prototype.PrototypeBean; public class SingletonFunctionBean { @Autowired - private Function beanFactory; + private Function beanFactory; - public PrototypeBeanWithParam getPrototypeInstance(String name) { - PrototypeBeanWithParam bean = beanFactory.apply(name); + public PrototypeBean getPrototypeInstance(String name) { + PrototypeBean bean = beanFactory.apply(name); return bean; } 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 719e25eb51..68b4e6ff73 100644 --- a/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java +++ b/spring-core/src/test/java/com/baeldung/scope/PrototypeBeanInjectionIntegrationTest.java @@ -1,7 +1,6 @@ package com.baeldung.scope; import com.baeldung.scope.prototype.PrototypeBean; -import com.baeldung.scope.prototype.PrototypeBeanWithParam; import com.baeldung.scope.singletone.SingletonFunctionBean; import com.baeldung.scope.singletone.SingletonLookupBean; import com.baeldung.scope.singletone.SingletonObjectFactoryBean; @@ -70,8 +69,8 @@ public class PrototypeBeanInjectionIntegrationTest { SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class); SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class); - PrototypeBeanWithParam firstInstance = firstContext.getPrototypeInstance("instance1"); - PrototypeBeanWithParam secondInstance = secondContext.getPrototypeInstance("instance2"); + PrototypeBean firstInstance = firstContext.getPrototypeInstance("instance1"); + PrototypeBean secondInstance = secondContext.getPrototypeInstance("instance2"); Assert.assertTrue("New instance expected", firstInstance != secondInstance); } From fd34932f4cc5d625285e463c2501aa9bb8fa5105 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 19:16:55 +0300 Subject: [PATCH 3/6] remove extra import --- spring-core/src/main/java/com/baeldung/scope/AppConfig.java | 1 - 1 file changed, 1 deletion(-) 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 ee6ae1a924..e1395ba72f 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java @@ -1,7 +1,6 @@ package com.baeldung.scope; import com.baeldung.scope.prototype.PrototypeBean; -import com.baeldung.scope.prototype.PrototypeBeanWithParam; import com.baeldung.scope.singletone.SingletonAppContextBean; import com.baeldung.scope.singletone.SingletonBean; import com.baeldung.scope.singletone.SingletonFunctionBean; From 32a9212fc9bbfcfdd65aec485c47d79323f5f109 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 19:22:26 +0300 Subject: [PATCH 4/6] 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); + } + +} From 588f84aa83d83da90f08144f9a6d8e98e6a43a36 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 20:02:31 +0300 Subject: [PATCH 5/6] separate configs --- .../baeldung/{ => config}/scope/AppConfigFunctionBean.java | 3 +-- .../src/main/java/com/baeldung/scope/AppConfig.java | 2 +- .../scope/PrototypeFunctionBeanIntegrationTest.java | 7 +++---- 3 files changed, 5 insertions(+), 7 deletions(-) rename spring-core/src/main/java/com/baeldung/{ => config}/scope/AppConfigFunctionBean.java (89%) diff --git a/spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java b/spring-core/src/main/java/com/baeldung/config/scope/AppConfigFunctionBean.java similarity index 89% rename from spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java rename to spring-core/src/main/java/com/baeldung/config/scope/AppConfigFunctionBean.java index 80779b62fd..a3c5445698 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfigFunctionBean.java +++ b/spring-core/src/main/java/com/baeldung/config/scope/AppConfigFunctionBean.java @@ -1,8 +1,7 @@ -package com.baeldung.scope; +package com.baeldung.config.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; 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 18c53fee15..948cd60375 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java @@ -13,7 +13,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration -@ComponentScan("com.baeldung.scope") +@ComponentScan(value="com.baeldung.scope") public class AppConfig { @Bean diff --git a/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java b/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java index 6c2309d86f..1e3c652599 100644 --- a/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java +++ b/spring-core/src/test/java/com/baeldung/scope/PrototypeFunctionBeanIntegrationTest.java @@ -9,19 +9,18 @@ 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.config.scope.AppConfigFunctionBean; import com.baeldung.scope.prototype.PrototypeBean; import com.baeldung.scope.singletone.SingletonFunctionBean; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = FactoryBeanAppConfig.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfigFunctionBean.class) public class PrototypeFunctionBeanIntegrationTest { - @Test public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn() { - AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); + AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfigFunctionBean.class); SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class); SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class); From 679336f3e9a83676389c9412d43bbe579c8adaec Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 24 Jun 2018 21:18:34 +0300 Subject: [PATCH 6/6] Update AppConfig.java --- spring-core/src/main/java/com/baeldung/scope/AppConfig.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 948cd60375..33a9c5c21e 100644 --- a/spring-core/src/main/java/com/baeldung/scope/AppConfig.java +++ b/spring-core/src/main/java/com/baeldung/scope/AppConfig.java @@ -5,7 +5,6 @@ import com.baeldung.scope.singletone.SingletonAppContextBean; import com.baeldung.scope.singletone.SingletonBean; import com.baeldung.scope.singletone.SingletonObjectFactoryBean; import com.baeldung.scope.singletone.SingletonProviderBean; - import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -13,7 +12,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration -@ComponentScan(value="com.baeldung.scope") +@ComponentScan("com.baeldung.scope") public class AppConfig { @Bean