1. 概述
AspectJ 主要是使用 XML 配置顾问方式自动为每个符合切入点表达式的类生成事务代理。创建测试操作步骤如下:
2. 创建测试项目
创建一个名为 aspectj-aop 项目,pom.xml
文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>hello-spring-transaction</groupId><artifactId>aspectj-aop</artifactId><version>1.0.0-SNAPSHOT</version><packaging>jar</packaging><properties><!-- 环境配置 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><!-- 统一的依赖管理 --><log4j.version>1.2.17</log4j.version><slf4j.version>1.7.25</slf4j.version><spring.version>4.3.17.RELEASE</spring.version><alibaba-druid.version>1.1.6</alibaba-druid.version><mysql.version>5.1.46</mysql.version><mybatis.version>3.2.8</mybatis.version><mybaits-spring.version>1.3.1</mybaits-spring.version><junit.version>4.12</junit.version><lombok.version>1.16.18</lombok.version></properties><dependencies><!-- Test Begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><!-- Test Begin --><!-- Spring Begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><!-- Spring End --><!-- Log Begin --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jul-to-slf4j</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- Log End --><!-- Database Begin --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${alibaba-druid.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybaits-spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><!-- Database End --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency></dependencies>
</project>
主要依赖: org.springframework:spring-aspects
2.1 创建实体类
TbContentCategory
package com.hello.spring.transaction.aspectsj.aop.domain;import lombok.Data;import java.util.Date;/*** 分类管理* <p>Title: TbContentCategory</p>* <p>Description: </p>** @author qfxl* @version 1.0.0* @date 2024/05/14*/
@Data
public class TbContentCategory {private Long id;private String name;private Integer status;private Integer sortOrder;private Boolean isParent;private Date created;private Date updated;private TbContentCategory parent;
}
TbContent
package com.hello.spring.transaction.aspectsj.aop.domain;import lombok.Data;import java.util.Date;/*** 内容管理* <p>Title: TbContent</p>* <p>Description: </p>** @author qfxl* @version 1.0.0* @date 2024/05/14*/
@Data
public class TbContent {private Long id;private String title;private String subTitle;private String titleDesc;private String url;private String pic;private String pic2;private String content;private Date created;private Date updated;private TbContentCategory tbContentCategory;
}
2.2 创建数据访问层
TbContentCategoryDao
package com.hello.spring.transaction.aspectsj.aop.dao;import com.hello.spring.transaction.aspectsj.aop.domain.TbContentCategory;
import org.springframework.stereotype.Repository;@Repository
public interface TbContentCategoryDao {void insert(TbContentCategory tbContentCategory);
}
TbContentDao
package com.hello.spring.transaction.aspectsj.aop.dao;import com.hello.spring.transaction.aspectsj.aop.domain.TbContent;
import org.springframework.stereotype.Repository;@Repository
public interface TbContentDao {void insert(TbContent tbContent);
}
2.3 创建业务逻辑层
TbContentCategoryService接口
package com.hello.spring.transaction.aspectsj.aop.service;import com.hello.spring.transaction.aspectsj.aop.domain.TbContent;
import com.hello.spring.transaction.aspectsj.aop.domain.TbContentCategory;public interface TbContentCategoryService {void save(TbContentCategory tbContentCategory, TbContent tbContent);
}
实现
package com.hello.spring.transaction.aspectsj.aop.service.impl;import com.hello.spring.transaction.aspectsj.aop.dao.TbContentCategoryDao;
import com.hello.spring.transaction.aspectsj.aop.domain.TbContent;
import com.hello.spring.transaction.aspectsj.aop.domain.TbContentCategory;
import com.hello.spring.transaction.aspectsj.aop.service.TbContentCategoryService;
import com.hello.spring.transaction.aspectsj.aop.service.TbContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class TbContentCategoryServiceImpl implements TbContentCategoryService {@Autowiredprivate TbContentCategoryDao tbContentCategoryDao;@Autowiredprivate TbContentService tbContentService;public void save(TbContentCategory tbContentCategory, TbContent tbContent) {tbContentCategoryDao.insert(tbContentCategory);tbContentService.save(tbContent);}
}
TbContentService接口
package com.hello.spring.transaction.aspectsj.aop.service;import com.hello.spring.transaction.aspectsj.aop.domain.TbContent;public interface TbContentService {void save(TbContent tbContent);
}
实现
package com.hello.spring.transaction.aspectsj.aop.service.impl;import com.hello.spring.transaction.aspectsj.aop.dao.TbContentDao;
import com.hello.spring.transaction.aspectsj.aop.domain.TbContent;
import com.hello.spring.transaction.aspectsj.aop.service.TbContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class TbContentServiceImpl implements TbContentService {@Autowiredprivate TbContentDao tbContentDao;public void save(TbContent tbContent) {tbContentDao.insert(tbContent);}
}
3. 创建 Spring 配置
3.1 spring-context.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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"><context:annotation-config/><context:component-scan base-package="com.hello.spring.transaction.aspectsj.aop"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 配置事务通知 --><tx:advice id="myAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 配置顾问和切入点 --><aop:config><aop:pointcut id="myPointcut" expression="execution(* com.hello.spring.transaction.aspectsj.aop.service.*.*(..))" /><aop:advisor advice-ref="myAdvice" pointcut-ref="myPointcut" /></aop:config>
</beans>
3.2 spring-context-druid.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 加载配置属性文件 --><context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties"/><!-- 数据源配置, 使用 Druid 数据库连接池 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass --><property name="driverClassName" value="${jdbc.driverClass}"/><!-- 基本属性 url、user、password --><property name="url" value="${jdbc.connectionURL}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><!-- 配置初始化大小、最小、最大 --><property name="initialSize" value="${jdbc.pool.init}"/><property name="minIdle" value="${jdbc.pool.minIdle}"/><property name="maxActive" value="${jdbc.pool.maxActive}"/><!-- 配置获取连接等待超时的时间 --><property name="maxWait" value="60000"/><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000"/><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000"/><property name="validationQuery" value="${jdbc.testSql}"/><property name="testWhileIdle" value="true"/><property name="testOnBorrow" value="false"/><property name="testOnReturn" value="false"/><!-- 配置监控统计拦截的filters --><property name="filters" value="stat"/></bean>
</beans>
3.3 spring-context-mybatis.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"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"><!-- 配置 SqlSession --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><!-- 用于配置对应实体类所在的包,多个 package 之间可以用 ',' 号分割 --><property name="typeAliasesPackage" value="com.hello.spring.transaction.aspectsj.aop.domain"/><!-- 用于配置对象关系映射配置文件所在目录 --><property name="mapperLocations" value="classpath:/mapper/**/*.xml"/><property name="configLocation" value="classpath:/mybatis-config.xml"></property></bean><!-- 扫描 Mapper --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.hello.spring.transaction.aspectsj.aop.dao" /></bean>
</beans>
4. 创建 MyBatis 配置和映射文件
4.1 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 全局参数 --><settings><!-- 打印 SQL 语句 --><setting name="logImpl" value="STDOUT_LOGGING" /><!-- 使全局的映射器启用或禁用缓存。 --><setting name="cacheEnabled" value="false"/><!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --><setting name="lazyLoadingEnabled" value="true"/><!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --><setting name="aggressiveLazyLoading" value="true"/><!-- 是否允许单条 SQL 返回多个数据集 (取决于驱动的兼容性) default:true --><setting name="multipleResultSetsEnabled" value="true"/><!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --><setting name="useColumnLabel" value="true"/><!-- 允许 JDBC 生成主键。需要驱动器支持。如果设为了 true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false --><setting name="useGeneratedKeys" value="false"/><!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不映射 PARTIAL:部分 FULL:全部 --><setting name="autoMappingBehavior" value="PARTIAL"/><!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) --><setting name="defaultExecutorType" value="SIMPLE"/><!-- 使用驼峰命名法转换字段。 --><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session --><setting name="localCacheScope" value="SESSION"/><!-- 设置 JDBC 类型为空时,某些驱动程序 要指定值, default:OTHER,插入空值时不需要指定类型 --><setting name="jdbcTypeForNull" value="NULL"/></settings>
</configuration>
4.2 TbContentCategoryMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hello.spring.transaction.aspectsj.aop.dao.TbContentCategoryDao"><insert id="insert">INSERT INTO tb_content_category (`parent_id`,`name`,`status`,`sort_order`,`is_parent`,`created`,`updated`)VALUES(#{parent.id},#{name},#{status},#{sortOrder},#{isParent},#{created},#{updated});</insert>
</mapper>
4.3 TbContentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hello.spring.transaction.aspectsj.aop.dao.TbContentDao"><insert id="insert">INSERT INTO tb_content (`category_id`,`title`,`sub_title`,`title_desc`,`url`,`pic`,`pic2`,`content`,`created`,`updated`)VALUES(#{tbContentCategory.id},#{title},#{subTitle},#{titleDesc},#{url},#{pic},#{pic2},#{content},#{created},#{updated})</insert>
</mapper>
5. 创建系统配置文件
5.1 log4j.properties
log4j.rootLogger=INFO, console, filelog4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n
j5.2 dbc.properties
#============================#
#==== Database settings ====#
#============================## JDBC
# MySQL 8.x: com.mysql.cj.jdbc.Driver
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://192.168.75.134:3306/myshop?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456# JDBC Pool
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20# JDBC Test
jdbc.testSql=SELECT 'x' FROM DUAL
6. 创建测试类
package com.hello.spring.transaction.aspectsj.aop.service.test;import com.hello.spring.transaction.aspectsj.aop.domain.TbContent;
import com.hello.spring.transaction.aspectsj.aop.domain.TbContentCategory;
import com.hello.spring.transaction.aspectsj.aop.service.TbContentCategoryService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml", "classpath:spring-context-druid.xml", "classpath:spring-context-mybatis.xml"})
public class TestSpringTransaction {@Autowiredprivate TbContentCategoryService tbContentCategoryService;@Testpublic void test() {TbContentCategory tbContentCategory = new TbContentCategory();tbContentCategory.setId(1L);tbContentCategory.setName("测试事务分类");TbContent tbContent = new TbContent();tbContent.setTbContentCategory(tbContentCategory);// 在这里你可以将内容设置为超出数据库字段的存储范围来验证事务是否开启tbContent.setTitle("测试事务内容");tbContentCategoryService.save(tbContentCategory, tbContent);}
}
运行观察事务效果:
- 有事务:数据插入成功则两张表都存在数据,只要出现异常则两张表都没有数据
- 无事务:如果第一张表数据插入成功,但第二张表报错则第一张表的数据不会回滚