Let’s say that I have a class with a constructor that takes one or more arguments. Let’s also say that the arguments are expected to be som kind of input from the user. I.e the argument can not be known at compile-time or config-time, only at runtime. Should I define my class as a prototype spring bean or should I just create it with “new”.
If I should define it as a bean, how can I pass in the arguments?
This is problematic in Spring. If your class has no dependencies on other beans, just create it with
new. If you have a class depending on other Spring beans but still you want pass some runtime arguments, currently Spring does not support it.However have a look at SPR-7431 and my article about passing custom argument to
<lookup-methods/>. If all goes well, this feature should be part of Spring 3.2 and it should match your requirements. It basically allows creatingprototype-scoped beans while still passing some constructor argument.