java-ContextRefreshEvent的Spring ApplicationListener.如何在每个层次结构中仅调用一次?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-ContextRefreshEvent的Spring ApplicationListener.如何在每个层次结构中仅调用一次?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3543字,纯文字阅读大概需要6分钟。
内容图文
![java-ContextRefreshEvent的Spring ApplicationListener.如何在每个层次结构中仅调用一次?](/upload/InfoBanner/zyjiaocheng/679/209a51373ee84a798a0936b5384727ed.jpg)
一旦我的spring Web应用程序启动了所有的bean,就需要执行特定的过程.为此,我创建了一个ApplicationListener<ContextRefreshedEvent>.
但是,当我运行该应用程序时,它会被调用多次(因为我们拥有不同名称空间的上下文,例如mvc-servlet等),但是当所有上下文都正确初始化时,我只需要一次调用此特定的侦听器.
有没有办法实现我想要做的事情?
我正在使用Spring 3.1.0.RELEASE.
解决方法:
是的,有一种方法,但是可能有些棘手.您正在谈论的子上下文可能是为DispatcherServlet启动的上下文.如果其中有多个,则每个调度程序servlet将获得一个上下文.
Spring将这些委托给容器,这样就没有关于初始化的单点管理.首先,初始化根应用程序上下文,然后由容器初始化各种servlet.对于其中每一个,可能会引入另一个上下文.
幸运的是,servlet规范可以通过load-on-startup参数来解决
The load-on-startup element indicates that this servlet should be loaded
(instantiated and have its init() called) on the startup of the web application. The optional contents of these
element must be an integer indicating the order in
which the servlet should be loaded. If the value
is a negative integer, or the element is not
present, the container is free to load the servlet
whenever it chooses. If the value is a positive
integer or 0, the container must load and
initialize the servlet as the application is
deployed. The container must guarantee that
servlets marked with lower integers are loaded
before servlets marked with higher integers. The
container may choose the order of loading of
servlets with the same load-on-start-up value.
因此,您应该基本上做两件事:
>在每个Servlet上指定一个load-on-startup元素,并确保其中一个具有独特的较高编号
>确保您的听众抓住正确的事件
例
考虑以下(简化的)web.xml定义
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>anotherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/first/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>anotherServlet</servlet-name>
<url-pattern>/second/*</url-pattern>
</servlet-mapping>
</web-app>
检测正确的上下文
此设置将导致对收听者的3个呼叫.在这种情况下,anotherServlet是链中的最后一个,因此可以按以下方式进行标识:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext context = event.getApplicationContext();
if (context instanceof ConfigurableWebApplicationContext) { // sanity check
final ConfigurableWebApplicationContext ctx =
(ConfigurableWebApplicationContext) event.getApplicationContext();
if ("anotherServlet-servlet".equals(ctx.getNamespace())) {
// Run your initialization business here
}
}
}
如果您想了解其来源,请查看FrameworkServlet#initServletBean.
并不是说您仍然可以在此时抛出异常,并且这仍将阻止应用程序正确部署.
定购
最后,如果为该特定事件注册了多个侦听器,您还可以确保最后处理您的事件.为此,您需要实现Ordered接口:
public class YourListener implements ApplicationListener<ContextRefreshedEvent>, Ordered {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) { }
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
内容总结
以上是互联网集市为您收集整理的java-ContextRefreshEvent的Spring ApplicationListener.如何在每个层次结构中仅调用一次?全部内容,希望文章能够帮你解决java-ContextRefreshEvent的Spring ApplicationListener.如何在每个层次结构中仅调用一次?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。