In the spring mvc, notes the need to open the configuration file, usually a simple project can be divided into two configuration files, here tentatively called spring-mvc.xml with spring-context.xml. In which the spring-mvc.xml as a servlet configuration file, the main scanning Controller annotations, and the other as a global configuration file can be used to register bean.
In the spring configuration file, you can
<context:annotation-config />
To open the annotation scanning function, but this method causes the system to default to scan all of the notes, including the @ Controller, @ Service and so on, but such a configuration would lead to failure of the transaction, please refer to the reasons here: click to jump Gangster's blog
Thus requires two configuration files are loaded @Controller and other annotations.
1, is the first look at how to configure web.xml configuration file
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>mvc-DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--载入配置文件owlforest.properties-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:owlforest.properties" />
<!--只扫描Controller-->
<context:component-scan base-package="com.owlforest.www" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
3、spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--载入配置文件owlforest.properties-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:owlforest.properties" />
<!--不扫描Controller-->
<context:component-scan base-package="com.owlforest.www">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
Reproduced in: https: //my.oschina.net/u/4108765/blog/3059603