Sep 25th, 2007, 05:44 PM
dependency injection occurs only when the app starts, but not when method is called
i don't have any experience with spring, but following some tutorials, dependency injection doesn't seem to be working in my app. for example i have ClassA and inside it have private ClassB classB, and a setter for classB. in my applicationContext.xml, i have the bean created for classA.
my app is running under oc4j, and when i start the app i have a breakpoint at the setter for classB, and it hits the breakpoint and classB is passed in correctly. but when i execute a function to create a new instance of ClassA, classB is null inside and the setter never gets called.
Last edited by 123; Sep 26th, 2007 at 01:10 PM.
Sep 28th, 2007, 11:28 AM
Vanilla use of Spring is pure Java - there is no way to intercept a constructor called from outside Spring. If you want to have Spring inject dependencies into objects you create yourself, you need to add some capabilities to your virtual machine (look for the @Configurable documentation in the User Guide). Most people don't need to go that far - why do you need to create that object? Isn't it possible to use one that Spring has created for you?
Sep 28th, 2007, 11:33 AM
thanks for the response.
Originally Posted by Dave Syer
i didn't know that it wasnt possible to just do User = new User() and have it inject Address into the User object. the reason i wanted this was because our stack is like the following: Action (Struts 2) -> Service -> Business Class -> Dao, so in my action i was doing something like UserService uService = new UserService() and then did service.getUsers, and was thinking inside my service, the BusinessClass would already be populated. Since htat didn't work, i had to do something like UserService s = contextPath.getBean("userService"), and it'll inject all the dependencies for me.
But i found the spring plugin for struts 2 where it creates the Action objects from spring, so those will inject the UserService w/ a service object for me, and go down the stack and inject all it's dependencies.