spring 上下文被重复加载的有关问题记事

spring 上下文被重复加载的问题记事

项目环境:springmvc, spring, hibernate

 

事情是这样的,项目在启动时需要启动一个线程,执行扫描任务,因为不是例行性任务,所以不考虑quartz,要完成这个功能,可以有多种实现方法,比如:

 

1. 自定义监听器

public class StartupListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent event) {
    ServletContext context = event.getServletContext();
    ApplicationContext cxt = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    Scanner scanner = (Scanner) ctx.getBean("scanner ");
    scanner.start();
  }
}
<listener>
 <listener-class>com.blablah.StartupListener</listener-class>
</listener>

 

2. 实现spring InitializingBean 接口

@Component
public class ScanTask implements InitializingBean {
   @Autowired private Scanner scanner;
   public void afterPropertiesSet() throws Exception {
      scanner.start();
   }
}

 

可项目启动后却发现扫描线程被启动了两次,即有两个Scanner实例在运行,这造成了数据的混乱,这时的web.xml 配置是这样的:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml </param-value>
</context-param>
 
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

 

造成这一问题的原因,是spring的ContextLoaderListener与springmvc的DispatcherServlet在启动时重复加载了spring上下文,网上也有很多建议方案,比如:

Spring MVC – Beans loaded twice:http://www.mkyong.com/spring-mvc/spring-mvc-beans-loaded-twice

Spring - application Initialized twice? :

http://stackoverflow.com/questions/20704086/spring-application-initialized-twice

 

然而,在尝试两者的建议后,问题却依然没有解决。既然它们的目的都是加载上下文,那启动一个不就可以了吗,于是修改web.xml,使用DispatcherServlet加载spring和springmvc上下文信息,问题得到解决:

 

<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml,classpath:springmvc-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>