昨天弄到半夜,Spring2.5 Ibatis2.3 注解注入,配置事务管理不成功

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&amp;useUnicode=true&amp;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是注释掉了,取消注释),事务管理是起作用的。

 

代码有点长,但是大家应该一看就明白,昨天弄了一晚上,很着急。。 我到底哪里配错了啊??

 

4个回答

那你就得拆开注解了,

比如
TestController 放在a.b.c包下


UserService 放在a.b.u包下

在servletXML里配置



在applicationXML里配置



当然,如果你觉得麻烦完全可以





同时放在servletxml里和applicationxml试试
(我个人觉得spring作bean的层级关系上应该会处理好的,application是最高级,如果application找不到就找servlet下的bean,当然这只是个人估计,你可以先试试,不行就采用上面不同的包区分)

用注解的话,你需要在service类上加注解:@Transactional。如下:

@Service

@Transactional
public class UserServiceImpl implements UserService { ...}

注解就这样用的,在service类上配置之后里面每个方法都是事务性的,要不你就用配置文件配置。

你配置配错了
[code="xml"]

[/code]

要放在

applicationContext.xml:

而不是

test-servlet.xml:

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!