tags:

views:

2438

answers:

4

I'm an end-user of one of my company's products. It is not very suitable for integration into Spring, however I am able to get a handle on the context and retrieve the required bean by name. However, I would still like to know if it was possible to inject a bean into this class, even though the class is not managed by Spring itself.

Clarification: The same application which is managing the lifecycle of some class MyClass, is also managing the lifecycle of the Spring context. Spring does not have any knowledge of the instance of MyClass, and I would like to some how provide the instance to the context, but cannot create the instance in the context itself.

A: 

I think this post may provide you with some answers

It shows you how to inject beans into arbitrary POJO's

John Daly
There seems to be a lot of confusion in the resulting comments from this post. Have you gotten it to work? Is the lazy-init flag being set to true mean that it's going to search for an object instance in the JVM and not instatiate it if already exists?
Spencer K
This did not work in a basic example, as it seems to be the common case from the commenters on the post.
Spencer K
+1  A: 

suppose that u have the following dependency chain:

A --> B --> C --> x --> y -- > Z

A, B, C are spring managed beans (constructed and manged by sprign framework) x, y are really simple POJOs that constructed by your application, without spring assistance

now if you want that y will get a reference to Z using spring that you need to have a 'handle' to the spring ApplicationContext

one way to do it is to implement ApplicationContextAware interface . In this case I would suggest that either A, B or C will implement this interface and will store the applicationContext reference in a static member.

so lets take Class C for example:

class C implmenets ApplicationContextAware{
    public static ApplicationContex ac;
     void setApplicationContext(ApplicationContext applicationContext)  {
               ac = applicationContext;
     }
 .............
}

now, in class y you should have:

(Z)(C.ac.getBean("classZ")).doSomething()

HTH -- Yonatan

Yonatan Maman
How about: x and y has no dependency on C, and I wish to inject Z into x or y? It seems I would have to use the @Comparable tag in spring-context, then do some runtime weaving after the fact. So far I have not managed to get this working, and adds extra work. Looking for an easier way.
Spencer K
+1  A: 

Be careful that in oldest version of Spring, there is thread-safe problem with bean factory http://jira.springframework.org/browse/SPR-4672

+3  A: 

You can do this:

ApplicationContext ctx = ...
YourClass someBeanNotCreatedBySpring = ...
ctx.getAutowireCapableBeanFactory().autowireBeanProperties(
    someBeanNotCreatedBySpring,
    AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);

You can use @Autowired and so on within YourClass to specify fields to be injected etc.

David Tinker
I never got a chance to try this, but it does seem to come the closest to what I was looking for. It would be nice to give it a whirl.
Spencer K