Announcement Announcement Module
Collapse
No announcement yet.
Injecting non-spring dependencies Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting non-spring dependencies

    How do you inject a non-spring managed dependency into a spring created object?

    For example, imagining I have some library that will callback on some interface method that I am implementing:

    Code:
    public void onMsg(MyFunkyDataValue dv)
    {
        // code used to look like this
        LibrarySuppliedHelper helper = new LibrarySuppliedHelper();
        helper.setX(...);
        helper.setY(...);
        helper.setZ(...);
        helper.setDv(dv);
        helper.init();
        helper.process();
    
        // I want to use spring to wire up my X, Y, and Z and serve
        // me up a new LibrarySuppliedHelper().   I'm just not sure which
        // approach is best.   X, Y, Z can be configured and injected easily
        // the dv cannot.   so, (this is really on the fly, btw).   Here's one 
        // approach that comes to mind.   Spring is initing X, Y, and Z for me
        // I do the rest
        helper = (...) beanFactory.getBean("HELPER");
        helper.setDv(dv);
        helper.init();
        helper.process();
    
        // Here's another approach that might work.   I can even get spring to
        // call init for me
        helper = (...) beanFactory.getBean("HELPER", new Object[] {dv});
        helper.process();
    
        // If spring created this object, we may have been able to take another
        // approach and get spring to inject a custom non-singleton,  object  
        // factory that uses beanFactory.getBean so this class doesn't have to
        // factory already injected with with x, y, and z values.
        // so far, I'm most sold on this approach - only my implementation
        // of the non-singleton factory has any dependency on spring APIs
        helper = factory.create(dv);
        helper.process();
    }
    No matter what approach I use, however, the best approaches I've found seems to be centered on the use of beanFactory.getBean(bean, args).

    Any thought, suggestions, comments on this approach?

  • #2
    You can use AutowireCapableBeanFactory to configure existing beans in a variety of different ways. This may do what you want.

    Rob

    Comment


    • #3
      Thanks Rob!

      AutowireCapableBeanFactory is just what I was looking for.

      Comment


      • #4
        Rob,

        How does this work with objects that require declarative transaction management? Is it possible to apply transaction management on the objects that are created outside the spring factory?

        Regards, dee

        Comment


        • #5
          It certainly is possible to do that, just not in this fashion. Essentially, you need to create your own instance of the TransactionInterpceptor and then create your own transactional proxy using ProxyFactoryBean. Check out the AOP chapter in the reference manual for more details on using ProxyFactoryBean.

          Rob

          Comment


          • #6
            Thanks, I will check it out this weekend!

            Comment

            Working...
            X