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); + } + }