为什么 contextInitialized() 被多次调用?
我正在 Jboss 4.2.3.GA 上运行 Stripes Web 应用程序,并尝试在启动 JBoss 时调用一个方法。我创建了一个ServletContextListener,如下所示:
public class TimerContextListener implements ServletContextListener {
@Inject
private TimerManager timerManager;
public void contextInitialized(ServletContextEvent servletcontextevent) {
((Injector) servletcontextevent.getServletContext().getAttribute(GuiceServletContextListener.KEY)).injectMembers(this);
timerManager.stopAllTimers();
timerManager.startTimer();
}
public void contextDestroyed(ServletContextEvent servletcontextevent) {
}
}
并且我在web.xml中添加了一个条目,如下所示:
<listener>
<listener-class>com.lawless.web.servletContextListeners.TimerContextListener</listener-class>
</listener>
但是当我启动服务器时,contextInitialized()被调用3次。知道问题是什么吗?谢谢。
I'm running a Stripes web app on Jboss 4.2.3.GA and am trying to call a method when I start JBoss. I created a ServletContextListener like so:
public class TimerContextListener implements ServletContextListener {
@Inject
private TimerManager timerManager;
public void contextInitialized(ServletContextEvent servletcontextevent) {
((Injector) servletcontextevent.getServletContext().getAttribute(GuiceServletContextListener.KEY)).injectMembers(this);
timerManager.stopAllTimers();
timerManager.startTimer();
}
public void contextDestroyed(ServletContextEvent servletcontextevent) {
}
}
and I added an entry in web.xml like so:
<listener>
<listener-class>com.lawless.web.servletContextListeners.TimerContextListener</listener-class>
</listener>
but contextInitialized() is getting called 3 times when I start my server. Any idea what the issue could be? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想通了。它被调用了 3 次,因为我在 jboss-web.xml 中定义了 3 个虚拟主机。但不确定为什么会导致这种行为。如果有人能解释原因,我将不胜感激。
Ok I figured it out. It was being called 3 times because I had 3 virtual hosts defined in my jboss-web.xml. Not sure why it causes that behavior though. If anyone can explain the reason I would appreciate it.
每个 Web 应用程序只有一个 ServletContext。
ServletContext
将在部署应用程序时创建(3 个虚拟主机意味着部署到具有 3 个不同 IP 地址的 3 个不同主机)。一旦创建了ServletContext
,它将被同一应用程序中的所有servlet 和JSP 文件使用。ServletContext
在Web应用场景中也被称为应用程序范围变量。来源 - http://www.javabeat.net/2009/02/servletcontextlistener-example/
There will be only one
ServletContext
for each web application.ServletContext
will be created while deploying the application (3 Virtual Hosts means deploying to 3 different hosts with 3 different IP addresses). Once theServletContext
is created, it will be used by all the servlets and JSP files in the same application.ServletContext
is also called as the application scope variables in the web application scenario.Source - http://www.javabeat.net/2009/02/servletcontextlistener-example/