Spring Java Config: how to create a prototype-scoped @Bean with runtime arguments?

Spring Java Config: how to create a prototype-scoped @Bean with runtime arguments?

In Spring Framework's Java configuration, you can create a prototype-scoped @Bean with runtime arguments by using method arguments in your @Bean definition. Prototype scope means that a new instance of the bean is created each time it's requested. Here's how you can do it:

  • Define a Java configuration class where you create your prototype-scoped bean with runtime arguments:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration public class MyConfig { @Bean @Scope("prototype") // Set the scope to prototype public MyPrototypeBean myPrototypeBean(String argument1, int argument2) { // Create and configure the bean using the runtime arguments return new MyPrototypeBean(argument1, argument2); } } 

In this example:

  • We annotate the configuration class with @Configuration.
  • We define a @Bean method named myPrototypeBean with the desired runtime arguments as method parameters.
  • We set the scope of the bean to prototype using @Scope("prototype"). This ensures that a new instance is created for each request.
  • Create a class MyPrototypeBean that represents the prototype-scoped bean:
public class MyPrototypeBean { private String argument1; private int argument2; public MyPrototypeBean(String argument1, int argument2) { this.argument1 = argument1; this.argument2 = argument2; } // Getter and setter methods for the properties } 

Now, whenever you request the myPrototypeBean bean from the Spring application context, a new instance of MyPrototypeBean will be created with the provided runtime arguments.

Here's an example of how to retrieve the prototype-scoped bean with runtime arguments:

import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class)) { // Request the prototype-scoped bean with runtime arguments MyPrototypeBean bean1 = context.getBean(MyPrototypeBean.class, "argumentValue1", 42); MyPrototypeBean bean2 = context.getBean(MyPrototypeBean.class, "anotherArgument", 99); // Use the prototype beans System.out.println(bean1.getArgument1()); // Output: argumentValue1 System.out.println(bean1.getArgument2()); // Output: 42 System.out.println(bean2.getArgument1()); // Output: anotherArgument System.out.println(bean2.getArgument2()); // Output: 99 } } } 

In this example, we use the getBean method of the AnnotationConfigApplicationContext to request the prototype-scoped bean with different runtime arguments, and each time, a new instance is created with the provided arguments.


More Tags

android-alarms file-uri git-status nameof broadcasting salesforce git-rebase glusterfs embedded-documents assistant

More Java Questions

More Livestock Calculators

More Genetics Calculators

More Geometry Calculators

More Fitness Calculators