add prototype bean ex with function

This commit is contained in:
Loredana Crusoveanu 2018-06-24 13:03:31 +03:00
parent 0242d74b93
commit 1cea9ee088
6 changed files with 115 additions and 0 deletions

View File

@ -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<String, PrototypeBeanWithParam> 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();
}
}

View File

@ -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();
}
}

View File

@ -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<String, PrototypeBeanWithParam> prototypeBeanFactory() {
return runtimeArg -> prototypeBeanWithParam(runtimeArg);
}
}

View File

@ -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;
}
}

View File

@ -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<String, PrototypeBeanWithParam> beanFactory;
public PrototypeBeanWithParam getPrototypeInstance(String name) {
PrototypeBeanWithParam bean = beanFactory.apply(name);
return bean;
}
}

View File

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