SPRING-MVC 启动重复加载两次映射

如下web.xml示例:

part1.用spring的配置加载contextConfigLocation

2.配置spring-mvc的contextConfigLocation

 

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:spring-*.xml
		</param-value>
	</context-param>
---------------------------------------------------------------------------
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--查找controller位置的xml文件配置 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行 -->
			<param-value>/WEB-INF/classes/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>

 

非常不幸,part1+2不能互相替代, 并且,如果两部分的xml重复命中,比如spring-*,就会导致springmvc的映射,scan重复加载两次。从启动日志和启动时长可以看到。 补充:重复加载还会导致配置的task,schadule任务重复执行!

 

所以,合理的做法是,把spring-mvc的定义单独摘出来定义在web容器启动的时候加载,名称也换成servlet-mvc.xml避免被spring重复加载, 而在spring的core xml定义中,不要出现mvc的东东

	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--查找controller位置的xml文件配置 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行 -->
			<param-value>/WEB-INF/classes/servlet-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>

 

	<!-- (Annotation方式配置services)enable component scanning (beware that this 
		does not enable mapper scanning!) -->
	<context:component-scan base-package="com.xx.controller" />

	<!-- enable autowire -->
	<context:annotation-config />

    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />

	<!-- 拦截器 -->
	<mvc:interceptors>
	    <mvc:interceptor>
	    	<mvc:mapping path="/*/**"/>
	        <bean class="com.xx.interceptors.DebugLogInterceptor" />
	    </mvc:interceptor>
	    
	    <mvc:interceptor>
	    	<mvc:mapping path="/user/**"/>
	    	<mvc:mapping path="/group/**"/>
	    	<mvc:mapping path="/groupalbum/**"/>
	    	<mvc:mapping path="/message/**"/>
	    	<mvc:exclude-mapping path="/user/login"/>
	    	<mvc:exclude-mapping path="/user/c/mobile"/>
	    	<mvc:exclude-mapping path="/user/c/email"/>

	        <bean class="com.hoomic.interceptors.LoginInterceptor" />
	    </mvc:interceptor>
	    
	</mvc:interceptors>

 

你可能感兴趣的:(SPRING-MVC 启动重复加载两次映射)