Java – use auto assembled beans in objects created at run time
I have a class B that implements the w interface It has a default implementation of the W method Classes C and D override the default implementation. They require a service whose bean is instantiated by spring Strings a and B come from the user, so I can't create B / C / D beans in advance So I have a factory that creates a new object based on user parameters (it will create B / C / D Based on parameters) What clean methods can I use the service beans (AA / BB / cc / DD, etc.) inside C and D (the spring is automatically assembled during server startup, and the parameters required to instantiate B / C / D are not available) or is there a better way to solve this problem?
Class B implements W{
String a;
String b;
B (String a,String b);
w_method(){
}
}
Class C extends B {
@Autowired
Service aa;
@Autowired
Service bb;
@Autowired
Service cc;
@override
w_method(){
}
}
Class D extends B {
@Autowired
Service dd;
@override
w_method(){
}
}
Solution
I think the easiest way is to connect the service to the factory and invoke setter on the B / C / D object before returning, instead of trying to use @Autowired..
Or use the constructor parameter method of axtavt If you want to avoid relying on ApplicationContext, you can use lookup method injection, but you must release a patch according to this blog post: http://nurkiewicz.blogspot.co.uk/2010/08/creating-prototype-spring-beans-on.html
