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:
sachin 2024-03-25 04:57:58 +05:30 committed by GitHub
parent e0a6bc2da3
commit 7c0faeef86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 172 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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