Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ public SingletonAppContextBean singletonAppContextBean() {
public SingletonObjectFactoryBean singletonObjectFactoryBean() {
return new SingletonObjectFactoryBean();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.baeldung.scope.prototype.PrototypeBean;
import com.baeldung.scope.singletone.SingletonBean;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;

Expand All @@ -19,4 +20,5 @@ public PrototypeBean prototypeBean() {
public SingletonBean singletonBean() {
return new SingletonBean();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,4 +59,5 @@ public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() {

Assert.assertTrue("New instance expected", firstInstance != secondInstance);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}