Announcement Announcement Module
No announcement yet.
Using IOC for non-spring managed beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using IOC for non-spring managed beans

    This is probably a question that has been asked several times before but I can’t seem to find it anywhere so I apologize if this is a dumb questions. I have class that is instantiated by a third-party library so I do not have control of when or how it is initialized.

    I would like this class to have a reference to a couple of my spring managed beans. However, I cannot my class cannot be a spring managed bean itself. Obviously, I can just use the web application context.

    WebApplicationContextUtils.getRequiredWebApplicati onContext(servletContext).getBean(“myManager”);

    However, this is not very “spring-like” and I was hoping there was some best practice type way to accomplish this. Any help is appreciated.


  • #2
    I have not done this before, but I think the FactoryBean[1] is what you need here. Google for examples.




    • #3
      Thanks for the reply. Your suggestion is actually the reverse of what I need. The FactoryBean interface allows me to dependency inject objects created from other contexts.

      In my case, I need to dependency inject beans created in the spring context into an object created from another context.


      • #4
        I believe this to be what you're looking for:

        Note you'll have to use AspectJ load-time weaving for this.


        • #5
          There are two threads that I am aware of that deal with registering external beans into an existing Spring context:


          I guess for this to be applicable to your scenario, the beans you want to wire must be "autowireable" by Spring, ie., have the required setters.