Announcement Announcement Module
Collapse
No announcement yet.
Callbacks and Dependency Injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Callbacks and Dependency Injection

    I come across this problem again and again. Various frameworks out of my control implement configuration for extension points by asking for a FQCN. Sometimes the class implements a particular interface, sometimes I have to configure the method name to call, sometimes the method is annotated. EJB3 EntityListener is one example of this.

    Almost invariably, the framework in question invokes my extension class with Reflection. My bean is instantiated by the JVM outside of the Spring container's knowledge. Typically, the framework considers the class to be stateless, but the business rules I have to model are very stateful indeed. I need information from an outside context.

    What I would like to do is implement these classes like Servlets. The Servlet itself is technically stateless, thread-safe, reentrant, etc., but is not a "function" per se, as it can permeate side-effects (through Session, Application, etc.) and not produce the same output for the same set of inputs.

    I would like my extension class to access these "stateful objects" via dependency injection (probably setter injection). Problem is, how can I use Spring to fulfill these dependencies when the object has already been instantiated? Even if I delegated the business rules to another bean that *is* managed by Spring, how do I get to the BeanFactory?

  • #2
    Hi toddiuszho

    Spring has built-in support to enable the dependency injection of objects that are not created by the Spring container.

    If you are using Java5, you can make use of the @Configurable annotation and AspectJ to dependency inject an object, as described here in the Spring reference documentation.

    If the @Configurable approach isn't floating your boat, the following links to the Spring API Javadoc may be of interest.

    The three links above are of course only useful if your code can get a handle on the Spring container (typically an ApplicationContext). There are a variety of approaches to doing this, from creating an explicit (gasp!) Singleton-per-ClassLoader to hold a reference to your container, or use the Spring-provided ContextSingletonBeanFactoryLocator.

    Ping this post back if you want any more in depth discussion of the above functionality.

    Cheers
    Rick

    Comment

    Working...
    X