Announcement Announcement Module
Collapse
No announcement yet.
dependency injection occurs only when the app starts up.. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • dependency injection occurs only when the app starts up..

    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.

    any suggestions?

    thanks
    Last edited by 123; Sep 26th, 2007, 02:10 PM.

  • #2
    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?

    Comment


    • #3
      Originally posted by Dave Syer View Post
      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?
      thanks for the response.

      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.

      thanks

      Comment

      Working...
      X