网上查了下了发现是spring的<context:component-scan />标签使用有问题
如果是搭建Spring3MVC,一般情况下在web.xml中有2个地方要加载spring的xml文件:
第一个地方:
<servlet> <servlet-name>bms</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/spring/servlet/servlet-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>bms</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
第二个地方:
<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>
第一个地方是专门用来解析springmvc控制层用的,也就是controller
第二个地方是解析除controller以外的一些bean,比如datasource,service,dao,事务等
本来我们只需在第一个地方做如下配置就行了:<context:component-scan base-package="com.xx" />
但是这样配置的话对事务不起作用
因此,我们要做如下配置:
去掉原先的<context:component-scan base-package="com.xx" />东东
在第一个地方配置:
<context:component-scan base-package="com.xx" use-default-filters="false"> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan>
这样配置就是让spring容器只扫描controller层
然后在第二个地方配置:
<context:component-scan base-package="com.xx"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan>
从字面上就看的出来,是让spring容器只扫描除了controller层以外的bean
然后还要添加几个jar包,不然启动会报asm,aspectj一些错误,需要添加的jar包如下(版本不一大致相同即可):
asm-2.2.3.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
如果启动报proxy10 cannot be cast to 的错误需要在第二个地方添加:
<aop:aspectj-autoproxy proxy-target-class="true" />
最后贴上我的配置代码:
我的项目是spring3mvc + ibatis
总体的:
jar包截图:
web.xml
<servlet> <servlet-name>bms</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/spring/servlet/servlet-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>bms</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <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>
applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <import resource="classpath*:/spring/data/*.xml" /> </beans>
database-config.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:property-placeholder location="classpath:/server.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="maxActive" value="${jdbc.maxActive}"></property> <property name="validationQuery" value="SELECT 1"></property> <property name="testOnBorrow" value="true"></property> <property name="testOnReturn" value="false"></property> <property name="testWhileIdle" value="true"></property> <property name="maxIdle" value="${jdbc.maxIdle}"></property> <property name="defaultAutoCommit" value="true" /> <property name="removeAbandoned" value="true" /> <property name="removeAbandonedTimeout" value="60" /> <property name="logAbandoned" value="false" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations"> <list> <value>classpath:/ibatis/sqlMapConfig.xml</value> </list> </property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 不加这个会报proxy10 cannot be cast to 的错误 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- spring context配置文件 --> <context:component-scan base-package="com.thc.bms"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan> </beans>
servlet-config.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx <context:component-scan">http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com.thc.bms" use-default-filters="false"> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan> <!-- 支持spring mvc新的注解类型 详细spring3.0手册 15.12.1 mvc:annotation-driven --> <mvc:annotation-driven /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>