How do you create a prototype-scoped @Bean with runtime arguments? With getBean(String name, Object... args)
?
My question is a consequence of this question.
Why is this approach not used or mentioned in the Spring IoC documentation?
Is this a normal approach? Is there a more correct approach for create a prototype @Bean with runtime arguments?
If it is not normal approach, so could you explain why? Pay attention, what i need set my arguments through constructor, not through setters.
@Autowired
private ApplicationContext appCtx;
public void onRequest(Request request) {
//request is already validated
String name = request.getParameter("name");
Thing thing = appCtx.getBean(Thing.class, name);
//System.out.println(thing.getName()); //prints name
}
-
public class Thing {
private final String name;
@Autowired
private SomeComponent someComponent;
@Autowired
private AnotherComponent anotherComponent;
public Thing(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}