【问题标题】:Use a ContextLoaderListener in accordance with DispatchServlet按照 DispatcherServlet 使用 ContextLoaderListener
【发布时间】:2010-04-06 06:50:33
【问题描述】:

我想同时使用 ContextLoaderListener(这样我就可以将 Spring Beans 传递给我的 servlet)以及 DispatchServlet(Spring MVC)。但是,目前我必须将 init 参数传递给这两个类初始化器:

<param-name>contextConfigLocation</param-name>
<param-value>
    /WEB-INF/spring/app-config.xml
</param-value>

所以,我对这两个类使用相同的 xml。想知道这是否会导致我的 bean 被初始化两次?如果是,我将如何避免这种情况?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    对于ContextLoaderListenerDispatcherServletcontextConfigLocation 参数是可选的。

    ContextLoaderListener 默认为/WEB-INF/application.xmlDispatcherServlet 默认为/WEB-INF/servletname-servlet.xml

    如果您明确设置这些参数,您应该将它们设置为相同的值。 ContextLoaderListenerDispatcherServlet 应该具有具有不同 bean 定义集的上下文,否则,正如您所说,bean 将被实例化两次。

    【讨论】:

    • 那么,这两者没有办法共享同一套bean吗?
    • @Phuong:servlet 的 bean 将有权访问侦听器加载的上下文中的 bean。上下文形成父子关系。 bean 由父“拥有”,但对子可见
    • 嗯,我在这里读到了你的一篇文章:stackoverflow.com/questions/1464881/…,问题解释得更清楚了。我会试着看看我是否可以避免重复我的豆子。谢谢。
    • 您好@skaffman 我已经尝试应用您的建议g,但是我在使用带有这样的文件的资源&lt;mvc:resources mapping="/content/games/**" location="file:${admin.content.path}/Games/Images/" /&gt; 时遇到了问题。我已经在这里发布了一个帖子stackoverflow.com/questions/9311741/…。如果你能调查一下,我将不胜感激。谢谢
    【解决方案2】:

    要强制 DispatcherServlet 初始化使用 ContextLoaderListener 中的 context,您应该将 contextConfigLocation 设置为空:

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

    【讨论】:

      【解决方案3】:

      这个也可以试试 --in bean context 排除控制器扫描

      <context:annotation-config/>
      <context:component-scan base-package="com.test.example">
          <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
      

      在 dispatcher servle 上下文中,只扫描控制器

      <context:component-scan base-package="com.test.example"  use-default-filters="false">
          <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
      
      <context:annotation-config/>
      

      https://www.concretepage.com/spring/spring-component-scan-include-and-exclude-filter-example

      【讨论】:

        猜你喜欢
        • 2012-06-14
        • 2016-09-12
        • 2012-03-07
        • 1970-01-01
        • 2020-03-31
        • 1970-01-01
        • 2012-09-01
        • 2013-12-15
        • 2017-08-01
        相关资源
        最近更新 更多