diff --git a/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/Employee.java b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/Employee.java new file mode 100644 index 0000000000..01eeb97936 --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/Employee.java @@ -0,0 +1,26 @@ +package com.baeldung.prototypebean.dynamicarguments; + +public class Employee { + + private String name; + + public Employee() { + } + + public Employee(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void printName() { + System.out.println(name); + } + +} diff --git a/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingFunction.java b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingFunction.java new file mode 100644 index 0000000000..02c283214c --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingFunction.java @@ -0,0 +1,17 @@ +package com.baeldung.prototypebean.dynamicarguments; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cglib.core.internal.Function; +import org.springframework.stereotype.Component; + +@Component +public class EmployeeBeanUsingFunction { + + @Autowired + private Function beanFactory; + + public Employee getEmployee(String name) { + Employee employee = beanFactory.apply(name); + return employee; + } +} diff --git a/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingLookUp.java b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingLookUp.java new file mode 100644 index 0000000000..2bdcc41e89 --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingLookUp.java @@ -0,0 +1,14 @@ +package com.baeldung.prototypebean.dynamicarguments; + +import org.springframework.beans.factory.annotation.Lookup; +import org.springframework.stereotype.Component; + +@Component +public class EmployeeBeanUsingLookUp { + + @Lookup + public Employee getEmployee(String arg) { + return null; + } + +} diff --git a/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingObjectFactory.java b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingObjectFactory.java new file mode 100644 index 0000000000..4fcb90de59 --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingObjectFactory.java @@ -0,0 +1,14 @@ +package com.baeldung.prototypebean.dynamicarguments; + +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.beans.factory.annotation.Autowired; + +public class EmployeeBeanUsingObjectFactory { + + @Autowired + private ObjectFactory employeeObjectFactory; + + public Employee getEmployee() { + return employeeObjectFactory.getObject(); + } +} \ No newline at end of file diff --git a/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingObjectProvider.java b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingObjectProvider.java new file mode 100644 index 0000000000..e04f98278c --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeBeanUsingObjectProvider.java @@ -0,0 +1,14 @@ +package com.baeldung.prototypebean.dynamicarguments; + +import org.springframework.beans.factory.annotation.Autowired; + +public class EmployeeBeanUsingObjectProvider { + + @Autowired + private org.springframework.beans.factory.ObjectProvider objectProvider; + + public Employee getEmployee(String name) { + Employee employee = objectProvider.getObject(name); + return employee; + } +} diff --git a/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeConfig.java b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeConfig.java new file mode 100644 index 0000000000..ebbd68a22b --- /dev/null +++ b/spring-di-4/src/main/java/com/baeldung/prototypebean/dynamicarguments/EmployeeConfig.java @@ -0,0 +1,39 @@ +package com.baeldung.prototypebean.dynamicarguments; + +import org.springframework.cglib.core.internal.Function; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +@ComponentScan(basePackages = { "com.baeldung.prototypebean.dynamicarguments" }) +public class EmployeeConfig { + + @Bean + @Scope(value = "prototype") + public Employee getEmployee(String name) { + return new Employee(name); + } + + @Bean + public EmployeeBeanUsingObjectProvider employeeBeanUsingObjectProvider() { + return new EmployeeBeanUsingObjectProvider(); + } + + @Bean + public EmployeeBeanUsingObjectFactory employeeBeanUsingObjectFactory() { + return new EmployeeBeanUsingObjectFactory(); + } + + @Bean + public Function beanFactory() { + return name -> getEmployee(name); + } + + @Bean + public EmployeeBeanUsingFunction employeeBeanUsingFunction() { + return new EmployeeBeanUsingFunction(); + } + +} diff --git a/spring-di-4/src/test/java/com/baeldung/prototypebean/dynamicarguments/DynamicBeanUnitTest.java b/spring-di-4/src/test/java/com/baeldung/prototypebean/dynamicarguments/DynamicBeanUnitTest.java new file mode 100644 index 0000000000..0c6e6a7658 --- /dev/null +++ b/spring-di-4/src/test/java/com/baeldung/prototypebean/dynamicarguments/DynamicBeanUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.prototypebean.dynamicarguments; + +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 junit.framework.Assert; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = EmployeeConfig.class) +public class DynamicBeanUnitTest { + + @Test + public void givenPrototypeBean_WhenFunction_ThenNewInstanceReturn() { + + AbstractApplicationContext context = new AnnotationConfigApplicationContext(EmployeeConfig.class); + EmployeeBeanUsingFunction firstContext = context.getBean(EmployeeBeanUsingFunction.class); + EmployeeBeanUsingFunction secondContext = context.getBean(EmployeeBeanUsingFunction.class); + Employee firstInstance = firstContext.getEmployee("sachin"); + Employee secondInstance = secondContext.getEmployee("kumar"); + Assert.assertTrue(firstInstance != secondInstance); + } + + @Test + public void givenPrototypeBean_WhenLookup_ThenNewInstanceReturn() { + + AbstractApplicationContext context = new AnnotationConfigApplicationContext(EmployeeConfig.class); + EmployeeBeanUsingLookUp firstContext = context.getBean(EmployeeBeanUsingLookUp.class); + EmployeeBeanUsingLookUp secondContext = context.getBean(EmployeeBeanUsingLookUp.class); + Employee firstInstance = firstContext.getEmployee("sachin"); + Employee secondInstance = secondContext.getEmployee("kumar"); + Assert.assertTrue(firstInstance != secondInstance); + } + + @Test + public void givenPrototypeBean_WhenObjectProvider_ThenNewInstanceReturn() { + AbstractApplicationContext context = new AnnotationConfigApplicationContext(EmployeeConfig.class); + EmployeeBeanUsingObjectProvider firstContext = context.getBean(EmployeeBeanUsingObjectProvider.class); + EmployeeBeanUsingObjectProvider secondContext = context.getBean(EmployeeBeanUsingObjectProvider.class); + Employee firstInstance = firstContext.getEmployee("sachin"); + Employee secondInstance = secondContext.getEmployee("kumar"); + Assert.assertTrue(firstInstance != secondInstance); + } +}