为什么我的春季背景初始化两次?

时间:2015-08-15 08:03:59

标签: spring-mvc

我无法理解Spring MVC配置和初始化。我不确定何时使用注释以及何时不使用注释!

我的应用程序由两个战争模块构成。我不太关心在两者中复制bean容器。但是在启动时,两个模块都初始化了两次弹簧上下文,如从启动时从其中一个模块中获取的日志中所证明的那样:

[2015-08-15 03:49:51,416] Artifact frontend:war exploded: Deploy took 27,529 milliseconds
15:50:00.051 DEBUG com.ocscommerce.admin.configuration.WebConfig 53 <init> - Initialising WebConfig
15:50:06.409 DEBUG com.ocscommerce.admin.configuration.AppConfig 48 <init> - Loading application context
15:50:06.424 DEBUG com.ocscommerce.admin.configuration.SecurityConfig 54 <init> - Loading security config.
15:50:07.327 DEBUG com.ocscommerce.services.configuration.SolrConfig 43 <init> - Loading SOLR Config.
2015-08-15 15:50:08.194 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-08-15 15:50:08.200 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
15:50:11.269 DEBUG com.ocscommerce.admin.configuration.WebConfig 53 <init> - Initialising WebConfig
15:50:13.498 DEBUG com.ocscommerce.admin.configuration.AppConfig 48 <init> - Loading application context
15:50:13.503 DEBUG com.ocscommerce.admin.configuration.SecurityConfig 54 <init> - Loading security config.
15:50:13.999 DEBUG com.ocscommerce.services.configuration.SolrConfig 43 <init> - Loading SOLR Config.
2015-08-15 15:50:14.336 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-08-15 15:50:14.337 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue

为什么有4个memcached连接,我不确定,但这是另一天的问题......

今天我想弄清楚为什么我的上下文每个模块初始化两次......

Web.XML - 引用了appconfig以及xml中的webconfig类。每个模块都有单独的web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/pages/errors/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/pages/errors/500.jsp</location>
    </error-page>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.ocscommerce.admin.configuration.AppConfig</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>

        </jsp-property-group>
    </jsp-config>

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                com.ocscommerce.admin.configuration.WebConfig
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter>
        <display-name>springMultipartFilter</display-name>
        <filter-name>springMultipartFilter</filter-name>
        <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springMultipartFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <display-name>springSecurityFilterChain</display-name>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>ERROR</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
</web-app>

然后在AppConfig类中,它会导入一些可能与问题无关的其他bean定义类。签名如下所示:

@Configuration
@PropertySource(value = {"classpath:application.properties"})
@EnableScheduling
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
@EnableTransactionManagement
@ComponentScan({"com.ocscommerce.admin", "com.ocscommerce.services"})
@Import(value = {SolrConfig.class, SecurityConfig.class, PersistenceConfig.class})
public class AppConfig {
}

然后 WebConfig 扩展了WebMvcAdapter并具有以下组件:

@Configuration
@EnableWebMvc
@ComponentScan({"com.ocscommerce.admin", "com.ocscommere.services"})
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableCaching
public class WebConfig extends WebMvcConfigurerAdapter {
}

我想知道我的组件扫描是否有太多?

有人可以帮我修复此配置吗?

谢谢...

1 个答案:

答案 0 :(得分:1)

我认为您已回答了自己的问题,您正在网络配置和应用配置中进行组件扫描。

重构组件扫描中的软件包名称,以便Web配置仅获取与Web层相关的任何bean,并且app config仅选择应用程序bean(例如服务)。请记住,使用contextloaderlistener,webconfig将能够看到应用程序配置中的bean。