Spring xml配置和注解一起使用

Spring xml和注解混用,方法的事物既有注解方式@Transactional()、又有xml的方式

<!-- 配置事务管理器类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务增强(如果管理事务?) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert*"/>
            <tx:method name="deleteByPrimaryKey" propagation="REQUIRES_NEW"/>
        </tx:attributes>
    </tx:advice>

    <!-- Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.hive.quartz.service.*.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
不会出现注解生效一部分,xml生效一部;xml的配置会覆盖注解配置。


Spring原文:
Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.

也就是说对于同一个bean 不能注解完成部分属性的设定 xml完成部分属性的设定。xml会覆盖注解

注意使用注解事物,事物里面的异常一定要抛出,不然会出现 Transaction rolled back because it has been marked as rollback-only异常

猜你喜欢

转载自blog.csdn.net/jing956899449/article/details/81357618
今日推荐