当我们启动一个web项目时,容器(tomcat)会先读取项目web.xml配置文件的配置,这一步骤完成后项目才正常启动。 在web.xml文件中的节点加载是有顺序,<context-param>-> <listener> -> <filter> -> <servlet>相同元素按照先后顺序加载。 我们首先在在web.xml中通过contextConfigLocation配置spring,contextConfigLocation定义spring配置文件的位置, 在applicationContext.xml中可以配置Bean,添加springMVC的Servlet节点,将springMVC的配置独立出来。这样在springmvc.xml中只对@Controller进行扫描就可,作为一个控制器,其他的事情不做。
在applicationContext.xml中只对一些事务逻辑的注解扫描。
web.xml配置<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>mes</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <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> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <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.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> <init-param> <param-name>sessionStatEnable</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <param-name>loginUsername</param-name> <param-value>druid</param-value> </init-param> <init-param> <param-name>loginPassword</param-name> <param-value>dream</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/views/error/500.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/WEB-INF/jsp/403.jsp</location> </error-page> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc.xml配置 在springmvc.xml配置中,我们需要配置<context:component-scan>,这样spring就可以自动去扫描base-pack下面或者子包下面的Java文件, 如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。 由于我们把spring的配置分开了,在这里只要配置Control的扫描就可以了<context:component-scan base-package="test.*.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan>
需要注意的是,在<context:component-scan>的参数中,use-default-filters默认为true,这时候他会对指定包下面全部带@注解标签的类注册为bean。<context:component-scan>有两个子标签,<context:include-filter>和<context:exclude-filter>
如果你只想扫描指定包下面的Controller时,需要设置use-default-filters为false,这时候使用<include-filter>才起效,<context:component-scan base-package="test.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan>
如果use-default-filters不设置为false,那你就需要把base-package精确到只含有control的包,不然他会扫描所有注解内容;
如果使用<exclude-filter>则不扫描该类注释,如果这里也设置了use-default-filters为false,则会导致不扫描,所以使用该标签时不需要设置false。