Reputation: 10215
I have a web application that loads a Spring context in the standard way:
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Unfortunately my application is not a "pure" web application, meaning that I don't have a real front end but just Akka remote actors that get initialized via a web.xml Listener as well. My Akka actors (standard POJOs) needs to get hold of the Spring context, so my question is: how do I get hold of the Spring context from a class that has no reference to the Servlet context?
Basically I cannot use:
WebApplicationContextUtils.getWebApplicationContext(ctx);
...because Akka actors have no reference to the ServletContext.
Thanks.
Upvotes: 0
Views: 711
Reputation: 26589
You can make your own Initializer that will create an instance of a SpringContextActor with a well known id, and then the actors needing to do something with the SpringContext can look the SpringContextActor in the ActorRegistry and send messages to it.
Upvotes: 1
Reputation: 308968
I'd wonder about a design that depended so strongly on the web tier.
The recommended Spring idiom is to have a POJO interface service layer that's called from the web tier. The benefit is that the service layer can be used even if the web tier is completely removed.
If you don't have a service layer, perhaps adding one would give you the flexibility to use it without the web tier or app server.
Upvotes: 0
Reputation: 597304
If these POJOs are spring beans, you can make them implement ApplicationContextAware
You can use @Configurable
to make any pojo a spring bean
If they are not spring beans, you can store the ServletContext
in JNDI on startup, and access it via JNDI.
Upvotes: 1