022:SpringBean的Aop的使用
1 SpringAop基本的概念
课题内容:
1.SpringAop注解方式基本API的实现
2.SpringBoot底层Aop的实现
3.基于SpringAop实现注解框架
什么是Aop编程
Aop面向切面编程,在方法之前和之后实现增强
应用场景在于:日志打印、事务实现、安全控制、计算方法耗时时间等。
AOP可以解决代码冗余问题
Spring的Aop的原理
前置通知/后置通知/环绕通知/运行通知/异常通知
Aop编程底层的原理
动态代理技术
— 基于Jdk实现InvocationHandler 底层使用反射技术 基于接口实现代理
— 基于CGLIB实现 字节码技术 基于类实现代理 继承模式
2 SpringAop注解方式的使用
Maven依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.11</version>
</dependency>
</dependencies>
基于注解方式启动Aop
@Component
public class OrderService {
public String addOrder(){
System.out.println("addOrder");
return "success";
}
}
@Configuration
@ComponentScan({"com.mayikt.service", "com.mayikt.aop"})
public class MyConfig {
}
@Aspect
@Component
@EnableAspectJAutoProxy
//@Import({AspectJAutoProxyRegistrar.class})
public class LogAop {
// @Aspect定义切面类
// @Pointcut 定义切入点:在方法拦截入口
// @EnableAspectJAutoProxy 开启aop
@Pointcut("execution (* com.mayikt.service..*.*(..))") // 所有类 所有方法 所有参数
public void logAop() {
}
@Before("logAop()")
public void doBefore() {
System.out.println("前置通知>>>在调用方法之前拦截");
}
@After("logAop()")
public void doAfter() {
System.out.println("后置通知>>>在调用方法之后拦截");
}
}
@Component
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
orderService.addOrder();
}
}
运行结果:
3 Spring五种通知的实现方式
@Aspect
@Component
@EnableAspectJAutoProxy
//@Import({AspectJAutoProxyRegistrar.class})
public class LogAop {
// @Aspect定义切面类
// @Pointcut 定义切入点:在方法拦截入口
// @EnableAspectJAutoProxy 开启aop
@Pointcut("execution (* com.mayikt.service..*.*(..))") // 所有类 所有方法 所有参数
public void logAop() {
}
@Before("logAop()")
public void doBefore() {
System.out.println("前置通知>>>在调用方法之前拦截");
}
@After("logAop()")
public void doAfter() {
System.out.println("后置通知>>>在调用方法之后拦截");
}
/**
* 环绕通知
*
* @param joinPoint
*/
@Around("logAop()")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(">>>>环绕通知 在方法之前执行...");
joinPoint.proceed();// 执行目标方法
System.out.println(">>>>环绕通知 在方法之后执行...");
}
/**
* 运行通知
*/
@AfterReturning("logAop()")
public void afterReturning(JoinPoint joinPoint) {
System.out.println("运行通知执行.....");
}
/**
* 异常通知
*
* @param joinPoint
*/
@AfterThrowing("logAop()")
public void afterThrowing(JoinPoint joinPoint) {
System.out.println(">>>>>异常通知");
}
}
运行结果:
当程序报错,执行异常通知,运行通知不执行,但后置通知仍然执行
4 Spring的事务的实现基本用法
@Service
public class OrderService2 {
@Autowired
private OrderInfoMapper orderInfoMapper;
@Transactional
public int addOrderInfo(int j) {
int i = orderInfoMapper.addOrderInfo();
int result = 1 / j;
return i;
}
}
方法上加上@Transactional开启事务,当参数 j=0,数据插入失败。
事务原理:当方法执行的时候如果抛出异常 就会回滚当前的事务;如果没有抛出异常,就提交事务。
5 SpringBoot手动的事务实现方式
@Component
public class TransactionalUtils {
/**
* 获取当前事务管理器
*/
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
public TransactionStatus begin(){
TransactionStatus transaction = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
System.out.println("获取当前事务...");
return transaction;
}
/**
* 提交事务
*/
public void commit(TransactionStatus transactionStatus){
System.out.println("提交当前事务...");
dataSourceTransactionManager.commit(transactionStatus);
}
/**
* 回滚事务
* @param transactionStatus
*/
public void rollback(TransactionStatus transactionStatus){
System.out.println("回滚当前事务...");
dataSourceTransactionManager.rollback(transactionStatus);
}
}
@Service
public class OrderService2 {
@Autowired
private OrderInfoMapper orderInfoMapper;
@Autowired
private TransactionalUtils transactionalUtils;
public int addOrderInfo(int j) {
TransactionStatus transaction = transactionalUtils.begin();
try {
int i = orderInfoMapper.addOrderInfo();
int result = 1 / j;
transactionalUtils.commit(transaction);
} catch (Exception e) {
e.printStackTrace();
transactionalUtils.rollback(transaction);
}
return 1;
}
}
6 使用SpringAOP重构实现声明事务
每个方法前后都加上以上方法,代码太冗余,基于Aop封装手动事务
@Aspect
@Component
@Scope("prototype")
public class TransactionalAop {
@Autowired
private TransactionalUtils transactionalUtils;
// transactionalUtils存在哪些问题?高并发情况下可能存在事务冲突问题 Aop切面要设置成多例
@Pointcut("execution (* com.mayikt.service..*.*(..))") // 所有类 所有方法 所有参数
public void transactionalAop() {
}
@Around("transactionalAop()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
TransactionStatus begin = transactionalUtils.begin();
try{
Object proceed = joinPoint.proceed();// 执行目标方法
transactionalUtils.commit(begin);
return proceed;
}catch (Exception e){
// 目标方法抛出异常,回滚当前事务
transactionalUtils.rollback(begin);
return 0;
}
}
}
@Service
public class OrderService2 {
@Autowired
private OrderInfoMapper orderInfoMapper;
public int addOrderInfo(int j) {
int i = orderInfoMapper.addOrderInfo();
int result = 1 / j;
return i;
}
}
7 Service方法使用Spring事务注意事项
@Service
public class OrderService3 {
@Autowired
private OrderInfoMapper orderInfoMapper;
public int addOrderInfo(int j) {
try {
int i = orderInfoMapper.addOrderInfo();
int result = 1 / j;
return i;
} catch (Exception e) {
e.printStackTrace();
// 手动进行回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return 0;
}
}
@Transactional
public int addOrderInfo02(int j) {
try {
int i = orderInfoMapper.addOrderInfo();
int result = 1 / j;
return i;
} catch (Exception e) {
// 手动进行回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
e.printStackTrace();
return 0;
}
}
// 程序抛出异常的情况下,会回滚吗?
}
注意:如果在service层抛出异常的情况下,最好手动回滚事务/或者自定义抛出异常。
8 自定义事务注解
自定义注解实现事务功能
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExtTransactional {
}
@Aspect
@Component
@Scope("prototype")
public class TransactionalAop2 {
@Autowired
private TransactionalUtils transactionalUtils;
// transactionalUtils存在哪些问题?高并发情况下可能存在事务冲突问题 Aop切面要设置成多例
@Pointcut("execution (* com.mayikt.service..*.*(..))") // 所有类 所有方法 所有参数
public void transactionalAop() {
}
@Around("transactionalAop()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
// 获取方法名称
String methodName = joinPoint.getSignature().getName();
// 获取目标对象
Class<?> classTarget = joinPoint.getTarget().getClass();
// 获取目标对象类型
Class<?>[] par = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
// 获取目标对象方法
Method objMethod = classTarget.getMethod(methodName, par);
// 判断该目标方法上是否有加上自定义事务注解
ExtTransactional extTransactional = objMethod.getDeclaredAnnotation(ExtTransactional.class);
if (extTransactional == null) {
return joinPoint.proceed();// 执行目标方法
}
TransactionStatus begin = transactionalUtils.begin();
try {
Object proceed = joinPoint.proceed();// 执行目标方法
transactionalUtils.commit(begin);
return proceed;
} catch (Exception e) {
// 目标方法抛出异常的情况下 回滚当前事务
transactionalUtils.rollback(begin);
return 0;
}
}
}
@Service
public class OrderService4 {
@Autowired
private OrderInfoMapper orderInfoMapper;
@ExtTransactional
public int addOrderInfo(int j) {
int i = orderInfoMapper.addOrderInfo();
int result = 1 / j;
return i;
}
}
测试结果: