SpringBoot中事务配置,springmvc项目升级spingboot踩坑

写这篇文章的原因就在于公司原来的springmvc项目升级成springboot项目,历经坎坷,眼看着准备上线了,发现事务配置没迁移过来。

大家都知道sptingboot事务配置非常简单,就在启动类上加上@EnableTransactionManagement注解,然后在service层加上@Transactional注解就行了,但是这样有一个缺点就是,每一个service那里都要写这个。

但是咱们这个项目是从mvc项目升级过来的,以前是xml方式统一配置的,这个就不好使了,然后我网上搜了一下怎么支持xml方式统一配置,直接把老项目的配置迁移过来,下面是网上给的教程:

1.添加transaction.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: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.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"></tx:method>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"></tx:method>
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"></tx:method>
        </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="allManagerMethod"
                  expression="execution (* com.neo.service.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" order="0"/>
    </aop:config>

    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

 

这样我们只需只要在编写事务代码的时候遵循上面的规则,编写方法名称,就可以对事务进行拦截。

2.在启动类上引入此配置文件。

@SpringBootApplication
@ImportResource("classpath:transaction.xml")
@MapperScan({"com.neo.dao"}) 
public class DemoApplication {

 

但是Spring Boot启动时,报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'org.springframework.transaction.config.internalTransactionalEventListenerFactory', defined in null, could not be registered. A bean with that name has already been defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

网上给的解决方法是这一样的:

原因:

启动类中的@EnableTransactionManagement与xml配置文件中的<tx:annotation-driven/>重复

 

解决方式:

1. 保留其中一个

2. 在Spring Boot配置文件中添加

spring:
  main:
    allow-bean-definition-overriding: true

我照着这个改了依旧报错:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Cannot resolve reference to bean 'allManagerMethod' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'allManagerMethod': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.aop.aspectj.AspectJExpressionPointcut] from ClassLoader [ParallelWebappClassLoader
  context: ROOT
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@48a242ce
]
找了半天,也不清楚是啥错,最后终于知道了是因为少了包,真是坑啊,我以为springboot这个全家桶肯定把相关的一些核心依赖都引了,结果发现不是,缺了切面相关的包,在pom文件引入如下依赖就好了:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>

总结:

springboot使用xml声明式事务配置如下:

在resources目录下增加transaction.xml文件

启动类加上如下注解:

application.yml文件加上如下配置:

spring:
  main:
    allow-bean-definition-overriding: true

最后关键的一步,也是坑我最惨的引入切面相关的依赖:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>

深感踩坑之路的艰辛,记录分享这篇文章,希望能帮到各位!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值