您的位置:首页 > 编程语言 > Java开发

spring事务配置在annotation不启作用,请问有什么解决办法或其它配置方法3

2016-10-13 13:49 537 查看
环境: 

spring版本2.5.6 

Hibernate 3.3.1.GA 

使用openSessionInView 

spring事务配置两种对使用XML都有效. 

AOP配置如下: 

Java代码  


<bean id="transactionManager"  

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">  

    <property name="sessionFactory" ref="sessionFactory" />  

</bean>  

  

<aop:aspectj-autoproxy proxy-target-class="true"/>  

  

<aop:config>  

      

       <aop:advisor pointcut="execution(* com.iit..service*..*(..))"  advice-ref="txAdvice" order="1"/>  

         

       <aop:advisor pointcut="execution(* com.iit..plugin*..*(..))"  advice-ref="txAdvice" order="2"/>  

</aop:config>  

  

<tx:advice id="txAdvice" transaction-manager="transactionManager">  

    <tx:attributes>  

        <tx:method name="get*" read-only="true" rollback-for="Throwable" />  

        <tx:method name="load*" read-only="true" rollback-for="Throwable"  />  

        <tx:method name="find*" read-only="true" rollback-for="Throwable" />  

        <tx:method name="*" rollback-for="Throwable" />  

    </tx:attributes>  

</tx:advice>  

transationInterceptor配置如下: 

Java代码  


<bean id="transationInterceptor"  

        class="org.springframework.transaction.interceptor.TransactionInterceptor">  

        <property name="transactionManager" ref="transactionManager" />  

        <property name="transactionAttributes">  

            <props>  

                <prop key="save*">PROPAGATION_REQUIRED</prop>  

                <prop key="update*">PROPAGATION_REQUIRED</prop>  

                <prop key="delete*">PROPAGATION_REQUIRED</prop>  

                <prop key="Save*">PROPAGATION_REQUIRED</prop>  

                <prop key="merge*">PROPAGATION_REQUIRED</prop>  

                <prop key="shield*" >PROPAGATION_REQUIRED</prop>  

            </props>  

        </property>  

  

    </bean>  

  

    <bean  

        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  

        <property name="beanNames">  

            <value>*ManagerImpl,*Dao,*Service,*ServiceImpl</value>  

        </property>  

        <property name="interceptorNames">  

            <list>  

                <value>transationInterceptor</value>  

            </list>  

        </property>  

    </bean>  

当service class使用XML配置时,事务是有效 

Java代码  


<bean id="testTxService" class="com.iit.test.service.impl.TestTxServiceImpl">  

    <property name="sessionFactory" ref="sessionFactory" />  

</bean>  

当使用annotation配置时,事务无效,不会生成代理. 

Java代码  


@Service("testTxManager")  

public class TestTxManagerImpl implements TestTxManager {  

    private SimpleHibernateDao<TestTx, Long> testTxDao;  

  

    @Required  

    @Autowired  

    public void setSessionFactory(@Qualifier("sessionFactory")  

    SessionFactory sessionFactory) {  

        testTxDao = new SimpleHibernateDao<TestTx, Long>(sessionFactory, TestTx.class);  

    }  

  

    public void saveTest() {  

        for (int i = 0; i < 3; i++) {  

            TestTx testTx = new TestTx();  

            testTx.setTxName("Kevin" + i);  

            testTxDao.merge(testTx);  

            if (i == 2) {  

                throw new RuntimeException("test");  

                // int k = 1/0;  

            }  

        }  

    }  

}  

且报下面错误: 

Java代码  


org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.  



请问怎么配置才能让事务在annotation生效?
 

问题补充:

使用cwx714的配置: 

Java代码  


<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  

有某此情况下是可以的(如下配置).但service还是需要写在XML文件中. 

Java代码  


<bean id="TestTxManager" class="com.iit.test.service.impl.TestTxManagerImpl">  

    <property name="sessionFactory" ref="sessionFactory" />  

</bean>  

Java代码  


<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->  

<context:component-scan base-package="com.iit.test.webapp.controller"/>  

请注意:一定要指定到controller这个目录下,如:com.iit.test.webapp.controller,而不能是整个com.iit 

这个配置在spring2.5的petclinic例子下,也是这样配置. 

petclinic的配置是这样的: 

Java代码  


<tx:annotation-driven />  

  

<!-- PetClinic's central data access object: Hibernate implementation-->   

<bean id="clinic" class="org.springframework.samples.petclinic.hibernate.HibernateClinic"/>  

Java代码  


<context:component-scan base-package="org.springframework.samples.petclinic.web" />  

为什么service要写在XML中呢? 

为什么扫描的包不能扩大呢? 

问题补充:

我找呀,找呀,终于找到答案了. 

事务配置成功. 

1.<context:component-scan base-package="com.iit.webapp"/>只能针对Web层, 

Service,DAO层不能包含在里面. 

如果有多个package,使用","分隔即可. 

2.在XML中需要声明service的名字与class,引用可以利用@Autowired注解来简化依赖注入.


按时间排序按投票排序

0spring版本2.5.6 
Hibernate 3.3.1.GA 
使用openSessionInView
0

在applicationContext.xml中加入: 

Java代码  


<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />  

在业务对象前加入: 

Java代码  


@Service  

@Transactional  

public class TestTxManagerImpl {  

  ...  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: