如果我将组件扫描放在spring-common.xml中并且Spring MVC找不到控制器,为什么?但是如果我将组件扫描放在springMVC-servlet.xml中并且它可以工作。
的web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-*.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC -->
<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:spring/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>
用SpringMVC-servlet.xml中
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
</bean>
<!-- 对静态资源文件的访问 -->
<mvc:resources mapping="/js/**" location="/static/js/" />
<mvc:resources mapping="/css/**" location="/static/css/" />
<mvc:resources mapping="/image/**" location="/static/image/" />
<mvc:default-servlet-handler />
弹簧common.xml
<context:component-scan base-package="com.wind">
</context:component-scan>
答案 0 :(得分:0)
即使你在项目中保留了spring-common.xml。您的容器(Tomcat,Jboss)将识别它们是否可供它们访问。似乎容器无法访问。
可以通过两种方式完成。
1)将spring-common,xml包含在web.xml的上下文配置位置中,如下所示
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVC-servlet.xml</param-value>
<param-value>classpath:spring/spring-common.xml</param-value>
</init-param>
2)从springMVC-servlet.xml
导入sprin-common.xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
</bean>
<import resource="spring-common.xml"/>
http://www.mkyong.com/spring/load-multiple-spring-bean-configuration-file/