Spring2.5+Ibatis2.3 用注解方式注入,事务管理不成功。。。。。
业务层:
@Service public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void insertUser() { User user = new User(); user.setName("a"); userDao.addUser(user); //第2次插入会出错 User user2 = new User(); user2.setName("bmmmmmmmmmmmmm"); userDao.addUser(user2); } }
DAO:
public class BaseDaoImpl extends SqlMapClientDaoSupport{ @Resource(name = "sqlMapClient") private SqlMapClient sqlMapClient; @PostConstruct public void initSqlMapClient(){ super.setSqlMapClient(sqlMapClient); } }
@Repository("userDao") public class UserDaoImpl extends BaseDaoImpl implements UserDao{ public UserDaoImpl() { super(); System.out.println("UserDaoImplUserDaoImplUserDaoImplUserDaoImpl"); } @Override public void addUser(User user) { this.getSqlMapClientTemplate().insert("User.addUser",user); } }
test-servlet.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.edgar" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> </beans>
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" xmlns:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1:3306/psimserver?autoReconnect=true&useUnicode=true&characterEncoding=GBK</value> </property> <property name="username"> <value>psim</value> </property> <property name="password"> <value>psim</value> </property> <property name="initialSize"> <value>50</value> </property> <property name="maxActive"> <value>1000</value> </property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:sql-map-config.xml</value> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- <bean id="userService" class="com.edgar.service.impl.UserServiceImpl"> <property name="userDao"> <ref bean="userDao" /> </property> </bean> <bean id="userDao" class="com.edgar.dao.impl.UserDaoImpl"> <property name="sqlMapClientTemplate"> <ref bean="sqlMapClientTemplate" /> </property> </bean> --> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* com.edgar.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config> <aop:aspectj-autoproxy /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" /> </tx:attributes> </tx:advice> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> </beans>
Service中第二次插入记录会报错,事务应该回滚,但是没有回滚
如果我把Service中的 @Resource 和 UserDaoImpl中的 @Repository("userDao") 这两个注解去掉,在applicationContext.xml中直接通过bean配置
(上面配置中userService和userDao是注释掉了,取消注释),事务管理是起作用的。
代码有点长,但是大家应该一看就明白,昨天弄了一晚上,很着急。。 我到底哪里配错了啊??