Prototype Scoped Bean With Runtime Arguments (#15936)
* Prototype Scoped Bean With Runtime Arguments * Prototype Scoped Bean With Runtime Arguments --------- Co-authored-by: Sachin kumar <sachin.n.kumar@oracle.com>
This commit is contained in:
parent
e0a6bc2da3
commit
7c0faeef86
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, Employee> beanFactory;
|
||||
|
||||
public Employee getEmployee(String name) {
|
||||
Employee employee = beanFactory.apply(name);
|
||||
return employee;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Employee> employeeObjectFactory;
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employeeObjectFactory.getObject();
|
||||
}
|
||||
}
|
|
@ -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<Employee> objectProvider;
|
||||
|
||||
public Employee getEmployee(String name) {
|
||||
Employee employee = objectProvider.getObject(name);
|
||||
return employee;
|
||||
}
|
||||
}
|
|
@ -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<String, Employee> beanFactory() {
|
||||
return name -> getEmployee(name);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EmployeeBeanUsingFunction employeeBeanUsingFunction() {
|
||||
return new EmployeeBeanUsingFunction();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue