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?