今天搭了个spring mvc项目,怎么也扫描不到controller,最后发现问题在use-default-filters="false"上面,乱copy出的问题
(默认值是true,它的作用就是扫描一些相关的注解,包括了@controller,@Component,@Service,@Repository等,)
<context:component-scan base-package="com.example" use-default-filters="false">
</context:component-scan>
如果配置为false,会在下面寻找类似配置include-filter和exclude-filter,一个黑名单方式,一个白名单方式,上面两个都没配,所以默认不扫描
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
application-context.xml是全局的,应用于多个serverlet,配合listener一起使用,web.xml中配置如下:
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
spring-mvc.xml 是spring mvc的配置,web.xml中配置如下:
<!--配置springmvc DispatcherServlet-->
<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:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
application-context.xml这个一般是采用非spring mvc架构,用来加载Application Context。
如果直接采用SpringMVC,只需要把所有相关配置放到spring-mvc.xml中就好,一般spring mvc项目用不到多个serverlet
一般它主要配置Controller的组件扫描器和视图解析器
springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- 使用注解开发,不用配置controller,需要配置一个组件扫描器 -->
<context:component-scan base-package="com.edu.test.controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根目录到指定目录一端路径 ,建议指定浅一点的目录-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2:applicationContext.xml配置要点(在web.xml文件需要加<listener>)
下为:applicationContext.xml文件
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置组件扫描器,使用注解方式开发,不用配置dao和service -->
<!-- 在springmvc.xml文件中也可以配置这个属性 -->
<context:component-scan base-package="com.edu.test"/>
<!-- 数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- 配置session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性 -->
<tx:attributes>
<!-- 添加事务管理的方法 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP,为添加事务管理的操作配置AOP -->
<aop:config>
<!-- 引入的Spring定义的事务通知,需要使用aop:advisor -->
<!-- 下面难 -->
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.edu.test.service.*.*(..))"
/>
</aop:config>
</beans>
3:在web.xml文件中,将springmvc.xml和applicationContext.xml一起引入
下为:web.xm文件
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 中央控制器 -->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置Spring提供的字符编码过滤器 -->
<filter>
<filter-name>SpringCharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
我们先看下这两个东东的配置方法:
对于contextConfigLocation参数,有2个地方可以配置:
1)context-param 是全局性配置
2)servlet下的init-param 是局部性配置
若以上两处都设置了一个相同的bean配置文件路径,那么该文件内配置的bean会被初始化2次,所以一个配置文件
只能选择一种配置位置;
项目中使用spring框架有2种方式:
1)listener下的ContextLoaderListener 是一种引入方式,默认读取
/WEB-INF/
applicationContext.xml
2)若是spring-web项目,DispatcherServlet 也是一种引入方式,默认读取
/WEB-INF/$
{servlet-name}-servlet.xml
倘若以上2种配置都引入了,那么全局性的bean配置文件会被加载2次;而且
2种方式的各自配置文件里的配置项在某种意义上并不是合并互补,而是各成一个体系(虽然普通的bean看似是都加载到全局上下文来了,但还是有一些特殊bean和配置项没有按预期的那样工作);
比如
/WEB-INF/
applicationContext.xml
文件下里的AOP声明式配置:
<!--aop 行为-->
<bean id="himvn" class="com.tangbao.hellomvn.Himvn" />
<!--aop 注释方式-->
<bean id="hiaspect" class="com.tangbao.hellomvn.Hiaspect" />
<!--aop config-->
<aop:aspectj-autoproxy />
<aop:config>
<aop:aspect id="aoplianxi" ref="himvn">
<aop:pointcut id="test1" expression="execution(* com.tangbao.controller.RestlessController.RestlessController(..))"></aop:pointcut>
<aop:before method="sayHi" pointcut-ref="test1"></aop:before>
<aop:after method="sayHi" pointcut-ref="test1"></aop:after>
</aop:aspect>
</aop:config>
若只是在全局配置项中,而没有在DispatcherServlet 中加载,那么此aop会无效。
所以,在web项目中,就不要使用ContextLoaderListener 和全局配置
contextConfigLocation参数了,统一在DispatcherServlet 下配置,应该就不那么混乱了。如下:
这样结果就如我们的预期:多bean配置文件不会出现重复加载,所有aop配置也都生效。
以上论点是在spring4.3.1下亲测所得,当然是从表现猜测本质的,还没有真正去研读Spring的源码,所以若有原理说错之处,还望各位看官指出!