获取加载 Spring 上下文的监听器

发布于 2024-10-07 18:42:03 字数 818 浏览 0 评论 0原文

我有一个以标准方式加载 Spring 上下文的 Web 应用程序:

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>

不幸的是,我的应用程序不是“纯”Web 应用程序,这意味着我没有真正的前端,而只是通过以下方式初始化的 Akka 远程 Actor还有一个 web.xml 监听器。 我的 Akka 参与者(标准 POJO)需要获取 Spring 上下文,所以我的问题是:如何从没有引用 Servlet 上下文的类中获取 Spring 上下文?

基本上我不能使用:

WebApplicationContextUtils.getWebApplicationContext(ctx);

...因为 Akka 参与者没有引用 ServletContext。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一紙繁鸢 2024-10-14 18:42:03

您可以创建自己的初始化程序,它将创建一个具有众所周知的 id 的 SpringContextActor 实例,然后需要使用 SpringContext 执行某些操作的 Actor 可以在 ActorRegistry 中查找 SpringContextActor 并向其发送消息。

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.

傲性难收 2024-10-14 18:42:03
  • 如果这些 POJO 是 spring bean,您可以让它们实现 ApplicationContextAware

  • 您可以使用@Configurable使任何pojo成为spring bean

  • 如果它们不是spring bean,您可以在启动时将 ServletContext 存储在 JNDI 中,并通过 JNDI 访问它。

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

优雅的叶子 2024-10-14 18:42:03

我想知道一个如此强烈依赖于网络层的设计。

推荐的 Spring 惯用法是拥有一个从 Web 层调用的 POJO 接口服务层。好处是即使完全删除Web层,服务层也可以使用。

如果您没有服务层,也许添加一个服务层可以让您在没有 Web 层或应用程序服务器的情况下灵活地使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文