做个学习笔记。SpringBoot创建的项目由于不存在xml配置文件了,对于用惯Spring的xml配置事务犯了难,百度了下,大多文章都是用@Transactional对每一个方法或类手动添加任务,这样很麻烦,就自己摸索了下,实现了对指定切点事务的统一添加。有两种方式。PS:启动类,对,就是包含main方法的那个类一定要放在包的最外层,不然有很多坑。包括但不限于不能扫描到你配置的类,连接ES时自定义接口无法自动注入等等。
1.Xml方式
跟Spring中差不多两步骤
①.在resources文件夹下创建xml文件。例如:transaction.xml
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">
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" >&l