Injecting beans into a class outside the Spring managed context

JavaSpring

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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 spring 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

Solution 3 - Java

One way to bring a bean into Spring despite its manufacture being external is to use a helper class marked as a @Configuration bean that has a method (marked with @Bean) that actually makes the instance and hands it back through Spring (which does its property injection and proxy generation at that point).

I'm not quite sure what scope you need; with prototype, you'll get a fresh bean in each place.

@Configuration
public class FooBarMaker {
    @Bean(autowire = Autowire.BY_TYPE)
    @Scope("prototype")
    public FooBar makeAFooBar() {
        // You probably need to do some more work in here, I imagine
        return new FooBar();
    }
}

You can inject properties required for manufacture into the @Configuration bean. (I use this to create instances of an interface where the name of the class to instantiate is defined at runtime.)

Solution 4 - Java

Another way to do this is to us use AspectJ. This is the recommended way of injection Spring beans into non-managed objects that are created with the new operator. See this for details:

http://www.javacodegeeks.com/2011/02/domain-driven-design-spring-aspectj.html

Solution 5 - Java

Searching endless combos of autowire inject spring bean into pojo applicationcontextaware beanaware etc circled me back here but this didnt provide a complete enough solution for me.

This is a much better implementation/tutorial of this IMO: I hope it helps everyone like it finally helped me.

Accessing Spring Beans from outside Spring Context

Solution 6 - Java

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

Solution 7 - Java

If you want to create an object outside the Spring context, and make that object available for injection into other beans that are in the Spring context, you can follow the steps in this article.

Basically, you create a parent application context and push your external object into this parent context as a singleton. Then you create you main application context (for example, from xml files), with the parent application context as its parent.

Object externalObject = ...
GenericApplicationContext parent = new StaticApplicationContext();
parent.getBeanFactory().registerSingleton( "externalObject", externalObject );
parent.refresh();
ApplicationContext appContext = new ClassPathXmlApplicationContext( ... , parent);

Solution 8 - Java

From a Spring configuration class, set a static field on the non-Spring class that needs the beans.

I have an example in my answer to a Liquibase question: https://stackoverflow.com/a/71191546/5499391

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpencer KormosView Question on Stackoverflow
Solution 1 - JavaDavid TinkerView Answer on Stackoverflow
Solution 2 - JavaYonatan MamanView Answer on Stackoverflow
Solution 3 - JavaDonal FellowsView Answer on Stackoverflow
Solution 4 - JavaHDaveView Answer on Stackoverflow
Solution 5 - Javauser1204808View Answer on Stackoverflow
Solution 6 - JavaView Answer on Stackoverflow
Solution 7 - JavaGreenGiantView Answer on Stackoverflow
Solution 8 - Javamouse_8bView Answer on Stackoverflow