Merge pull request #4542 from eugenp/prototype-bean-function

add prototype bean ex with function
This commit is contained in:
Loredana Crusoveanu 2018-07-29 21:58:24 +03:00 committed by GitHub
commit ca1908a351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 0 deletions

View File

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

View File

@ -40,4 +40,5 @@ public class AppConfig {
public SingletonObjectFactoryBean singletonObjectFactoryBean() {
return new SingletonObjectFactoryBean();
}
}

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

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

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.PrototypeBean;
public class SingletonFunctionBean {
@Autowired
private Function<String, PrototypeBean> beanFactory;
public PrototypeBean getPrototypeInstance(String name) {
PrototypeBean bean = beanFactory.apply(name);
return bean;
}
}

View File

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

View File

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