Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters dynamically to Spring beans

I am new to Spring.

This is the code for bean registration:

<bean id="user" class="User_Imple"> </bean> <bean id="userdeff" class="User"> </bean> 

and this is my bean class:

public class User_Imple implements Master_interface {      private int id;     private User user; // here user is another class      public User_Imple() {         super();     }      public User_Imple(int id, User user) {         super();         this.id = id;         this.user = user;     }      // some extra functions here.... } 

and this is my main method to perform action:

public static void main(String arg[]) {      ApplicationContext context = new ClassPathXmlApplicationContext("/bean.xml");     Master_interface master = (Master_interface)context.getBean("user");      // here is my some operations..     int id = ...     User user = ...      // here is where i want to get a Spring bean     User_Imple userImpl; //want Spring-managed bean created with above params } 

Now I want to call this constructor with parameters, and these parameters are generated dynamically in my main methods. This is what I mean by I want to pass dynamically – not statically, like declared in my bean.config file.

like image 449
Ramesh J Avatar asked Jun 08 '13 07:06

Ramesh J


People also ask

Can we use @bean without @configuration?

@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.

Does @bean have an ID attribute?

getBean("bean-identifier"); . Take @Bean, the java equivalent of <bean> tag, you wont find an id attribute. you can give your identifier value to @Bean only through name attribute.


1 Answers

If i get you right, then the correct answer is to use getBean(String beanName, Object... args) method, which will pass arguments to the bean. I can show you, how it is done for Java based configuration, but you'll have to find out how it is done for an XML based configuration.

@Configuration public class ApplicationConfiguration {          @Bean   @Scope("prototype")  // As we want to create several beans with different args, right?   String hello(String name) {     return "Hello, " + name;   } }  // and later in your application  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); String helloCat = (String) context.getBean("hello", "Cat"); String helloDog = (String) context.getBean("hello", "Dog"); 

Is this what are you looking for?


UPDATE

This answer gets too much upvotes and nobody looks at my comment. Even though it's a solution to the problem, it is considered as a Spring anti-pattern and you shouldn't use it! There are several different ways to do things right using factory, lookup-method, etc.

Please use the following SO post as a point of reference:

  • How to instantiate Spring managed beans at runtime?
like image 196
Vadim Kirilchuk Avatar answered Sep 22 '22 15:09

Vadim Kirilchuk