Spencer Kormos
Spencer Kormos

Reputation: 8451

Injecting beans into a class outside the Spring managed context

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.

Upvotes: 68

Views: 90058

Answers (7)

mouse_8b
mouse_8b

Reputation: 534

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

Upvotes: 0

Yonatan Maman
Yonatan Maman

Reputation: 2508

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

Upvotes: 3

GreenGiant
GreenGiant

Reputation: 5256

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);

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137787

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.)

Upvotes: 3

Dave
Dave

Reputation: 21924

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

Upvotes: 2

user1204808
user1204808

Reputation: 29

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

Upvotes: 1

David Tinker
David Tinker

Reputation: 9644

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.

Upvotes: 69

Related Questions