Spring与dubbo集成实例化两次的问题

最近研究dubbo,在与spring集成的时候,eclipse启动web服务的时候发现消费者实例总是被实例化两次,spring中bean默认都是singleton,不可能实例化多个对象。dubbo管理界面显示如下:

在网上查了相关的资料发现是由于在web.xml中这段配置导致的:
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:applicationContext.xml,classpath*:spring-web.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

Spring中监听器ContextLoaderListener的作用是什么呢?它是这样描述的:
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
由于DispatcherServlet会也实例化classpath*:applicationContext.xml,这就导致配置在applicationContext.xml中的消费者实例被初始化了两次。
解决方案:
删除这段代码即可:
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

猜你喜欢

转载自richard-lee.iteye.com/blog/2264732