Advanced Ways to Create Parameterized Beans at Runtime in Spring Boot

This article demonstrates five advanced techniques for creating Spring Boot prototype beans with runtime parameters—including @Bean with @Scope, ApplicationContext.getBean, @Lookup, Function beans, and ObjectProvider—providing code examples, usage notes, and output showing distinct bean instances.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Advanced Ways to Create Parameterized Beans at Runtime in Spring Boot

Spring Boot defines several bean scopes such as singleton, prototype, and request, with singleton being the default. When a new instance is needed for each use or when a prototype bean must receive dynamic parameters, the standard singleton approach is insufficient.

The article presents five concrete methods to obtain a prototype bean with runtime arguments.

1. Define a prototype bean with parameters

public class Employee {
  private String name;
  public Employee(String name) {
    this.name = name;
  }
  public void print() {
    System.out.println(this.name);
  }
}
@Configuration
public class AppConfig {
  @Bean
  @Scope(BeanDefinition.SCOPE_PROTOTYPE)
  public Employee employee(String name) {
    return new Employee(name);
  }
}

2. Retrieve the prototype bean via ApplicationContext

@Component
public class UseEmployeePrototype implements ApplicationContextAware {
  private ApplicationContext context;
  public void usePrototype() {
    Employee employee = (Employee) context.getBean("employee", "pack_xg");
    employee.print();
  }
  @Override
  public void setApplicationContext(ApplicationContext context) {
    this.context = context;
  }
}

This approach tightly couples bean creation to the ApplicationContext, which may affect maintainability if the bean implementation changes.

3. Use @Lookup for method injection

@Component
public class EmployeeLookup {
  @Lookup
  public Employee getEmployee(String arg) {
    return null; // Spring overrides to return the appropriate prototype bean
  }
}

4. Use a Function bean

@Component
public class EmployeeFunction {
  private final Function<String, Employee> employeeFactory;
  public EmployeeFunction(Function<String, Employee> employeeFactory) {
    this.employeeFactory = employeeFactory;
  }
  public Employee getEmployee(String name) {
    return employeeFactory.apply(name);
  }
}
@Configuration
public class AppConfig {
  @Bean
  @Scope(BeanDefinition.SCOPE_PROTOTYPE)
  public Employee employee(String name) {
    return new Employee(name);
  }
  @Bean
  public Function<String, Employee> ef() {
    return name -> employee(name);
  }
}

5. Use ObjectProvider&lt;T&gt;

@Component
public class EmployeeObjectProvider {
  private final ObjectProvider<Employee> objectProvider;
  public EmployeeObjectProvider(ObjectProvider<Employee> objectProvider) {
    this.objectProvider = objectProvider;
  }
  public Employee getEmployee(String name) {
    return objectProvider.getObject(name);
  }
}

Testing the approaches

@Component
public class PrototypeRunner implements CommandLineRunner {
  private final EmployeeLookup el;
  public PrototypeRunner(EmployeeLookup el) {
    this.el = el;
  }
  @Override
  public void run(String... args) throws Exception {
    Employee e1 = el.getEmployee("pack");
    System.err.println(e1);
    Employee e2 = el.getEmployee("xg");
    System.err.println(e2);
  }
}

The console output shows two distinct object identities, confirming that each call creates a new prototype instance:

com.pack.Employee@526e8108
com.pack.Employee@4dcbae55

Each method has trade‑offs: direct ApplicationContext access couples code to the container, @Lookup relies on Spring’s method‑level injection, the Function approach leverages Java 8 functional interfaces, and ObjectProvider offers a flexible, type‑safe way to obtain beans with arguments.

By selecting the appropriate technique, developers can create prototype beans with dynamic parameters while keeping the codebase clean and maintainable.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring Bootdynamic-parametersprototype-bean@lookupobjectproviderfunction-bean
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.