Using autowired beans in an object created at runtime
autowired, java, spring
Solution
I think the simplest method would be to wire the services into the factory and call setters on the B / C / D objects before you return them, rather than attempting to use `@Autowired`.
Or use axtavt's constructor argument method. If you want to avoid depending on `ApplicationContext`, you can use Lookup Method Injection, but you'll have to patch Spring per this blog post: http://nurkiewicz.blogspot.co.uk/2010/08/creating-prototype-spring-beans-on.html
Problem
I have a class B which implements W interface. It has a default implementation of W's method. class C and D override default implementation for which they need a service whose bean is instantiated by spring. String a and b comes from user and hence there is no way I can create a bean of B/C/D in advance. So I have a factory which creates a new object based on user parameters (it will create B/C/D based on parameters). Is there any clean way I can use service beans(aa/bb/cc/dd etc.) from inside C and D (spring autowires during server startup, at that time parameter required for instantiating B/C/D are not available) or is there any better way to solve the 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(){ } } ```