开发者

Get hold of a Listener loaded the Spring context

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>
             or开发者_如何学编程g.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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜