Merge pull request #4542 from eugenp/prototype-bean-function
add prototype bean ex with function
This commit is contained in:
commit
ca1908a351
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.config.scope;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
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<String, PrototypeBean> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -40,4 +40,5 @@ public class AppConfig {
|
|||
public SingletonObjectFactoryBean singletonObjectFactoryBean() {
|
||||
return new SingletonObjectFactoryBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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.PrototypeBean;
|
||||
|
||||
public class SingletonFunctionBean {
|
||||
|
||||
@Autowired
|
||||
private Function<String, PrototypeBean> beanFactory;
|
||||
|
||||
public PrototypeBean getPrototypeInstance(String name) {
|
||||
PrototypeBean bean = beanFactory.apply(name);
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.scope;
|
||||
|
||||
import com.baeldung.scope.prototype.PrototypeBean;
|
||||
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 +59,5 @@ public class PrototypeBeanInjectionIntegrationTest {
|
|||
|
||||
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
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.config.scope.AppConfigFunctionBean;
|
||||
import com.baeldung.scope.prototype.PrototypeBean;
|
||||
import com.baeldung.scope.singletone.SingletonFunctionBean;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfigFunctionBean.class)
|
||||
public class PrototypeFunctionBeanIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn() {
|
||||
|
||||
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfigFunctionBean.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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue