I have a instance of a class that is created outside of Spring that I'd like to have access to Spring beans so that it may fire an event and be observed by Spring beans. I'm not using Spring web, my application is running from the command-line via spring boot.
2 Answers
The only option you have is to expose the Spring context of your application using a static method so that the object that is not managed by Spring can use it to get references to managed beans it needs.
Start with a wrapper for the context. Create a regular managed bean which required reference to the context in its constructor. The reference is assigned to a static class field, which also has a static getter:
@Service class ContextWrapper { private static ApplicationContext context; @Autowired public ContextWrapper(ApplicationContext ac) { context = ac; } public static ApplicationContext getContext() { return context; } }
Use the static getter to get access to context in the object which is not managed by Spring and get reference to beans using methods available in the context:
SomeBean bean = ContextWrapper.getContext().getBean("someBean", SomeBean.class); // do something with the bean
Last thing you need is communication channel from Spring beans to non-managed object. For instance, the
SomeBean
can expose a setter which will accept the non-managed object as a parameter and store the reference in a field for future use. The object mast get references to managed beans using the static context accessor mentioned above and use the setter to make the bean aware of its existence.@Service class SomeBean { // ... your bean stuff private SomeClass someclass; public void setSomeClass(Someclass someclass) { this.someclass = someclass; } private void sendEventToSomeClass() { // communicate with the object not managed by Spring if (someClass == null) return; someClass.sendEvent(); } }

- 13,995
- 6
- 58
- 65
-
How can I make sure context is not null or not loaded completely? Can I rely that getContext() will return full spring context and why? – Ori Marko Nov 21 '17 at 07:31
-
If your Spring application has started, the context must be created correctly. The only thing that you have to remember is that you should not use the `getContext()` method in constructors of your beans, as beans construction is a part of context creation. – Daniel Olszewski Nov 21 '17 at 08:00
-
When calling from a **regular (non spring) class** for getting a bean, can I assume that spring context already started ? is there a need to add check/function to make sure spring context is not null or not fully loaded? – Ori Marko Nov 21 '17 at 08:23
-
I asked a new question https://stackoverflow.com/questions/47434743/get-spring-boot-bean-from-jersey-non-spring-context – Ori Marko Nov 22 '17 at 12:45
You can inject by constructor that spring beans, something like:
@Service
class Bean {
...
}
class NotBean {
private Bean bean;
public NotBean(Bean bean) {
this.bean = bean;
}
// your stuff (handle events, etc...)
}

- 65
- 1
- 8
-
1I don't have control on how the other NotBean is instantiated, therefore, I cannot simply pass the bean reference to it ... – Walter May 05 '17 at 12:29
-