AspectJ(面向切面编程框架)-- 注解版
AspectJ常用的注解
@Aspect 标注当前类为切面类
@Before 前置执行
@After 后置执行
@AfterReturning 后置返回数据执行
@AfterThrowing 后置异常处理执行
@Pointcut 切入点
依赖的引入
<!--
Spring 基础包括 :
Spring-core/Spring-beans/Spring-app/Spring-expression
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!--添加lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<!--aop框架: aspectj-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
目标类:userService/userServiceImpl (添加注解)
userService
package com.bboy.service;
public interface UserService {
public void addUser(String name,String password);
public int deleteUserById(int id);
public int updateUser();
}
userServiceImpl
package com.bboy.service.impl;
import com.bboy.service.UserService;
import org.springframework.stereotype.Service;
@Service("userService")
/*
* @类描述:
* @作者:秦帅
* @时间:2023/11/22 0022 14:41:11
*/
public class UserServiceImpl implements UserService {
@Override
public void addUser(String name, String password) {
}
@Override
public int deleteUserById(int id) {
return 10086;
}
@Override
public int updateUser() {
return 10085;
}
}
切面类:MyAspectJ (添加注解)
package com.bboy.aspect;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import sun.awt.SunHints;
@Component
@Aspect
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/22 0022 14:41:41
*/
public class MyAspectJ {
@Before("execution(* com.bboy.service.impl.UserServiceImpl.*(..))")
public void myBeforeMethod(JoinPoint joinPoint){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 方法参数列表内容
Object[] objs = joinPoint.getArgs();
for(Object obj:objs){
System.out.println(obj);
}
//- 具体的业务逻辑
System.out.println(methodName+"========>>开始执行");
}
@After("execution(* com.bboy.service.impl.UserServiceImpl.*(..))")
public void myAfterMethod(JoinPoint joinPoint){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 具体的业务逻辑
System.out.println(methodName+"========>>结束执行");
}
@AfterReturning(value = "execution(* com.bboy.service.impl.UserServiceImpl.*(..))",returning ="res")
public void myAfterReturning(JoinPoint joinPoint,Object res){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 具体的业务逻辑
System.out.println(methodName+"========>>返回的数据为:"+res);
}
@AfterThrowing(value = "execution(* com.bboy.service.impl.UserServiceImpl.*(..))",throwing ="ex")
public void myAfterThrowing(JoinPoint joinPoint,Throwable ex){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 具体的业务逻辑
System.out.println(methodName+"========>>返回的错误信息是:"+ex.getMessage());
}
}
修改核心配置文件
i. 引入context 命名空间(使用扫描)
ii. 引入aop命名空间
iii. 开启扫描
iv. 开启aop注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--
进行组件扫描,扫描含有注解的类
-->
<context:component-scan base-package="com.bboy.service.impl"/>
<context:component-scan base-package="com.bboy.aspect"/>
<!--开启aop的注解(确认 aop生效)-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
切面类进行优化:MyAspect 类优化
//- 声明公共的切入点
@Pointcut(value ="execution(* com.bboy.service.impl.UserServiceImpl.*(..))")
public void myPointCut(){}
//- 将切入点进行注入
@Before(value = "myPointCut()")
@After(value = "myPointCut()")
@AfterReturning(value = "myPointCut()",returning ="res")
@AfterThrowing(value = "myPointCut()",throwing ="ex")
package com.bboy.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/22 0022 14:41:41
*/
public class MyAspectJ_copy {
//- 声明公共的切入点
@Pointcut(value ="execution(* com.bboy.service.impl.UserServiceImpl.*(..))")
public void myPointCut(){}
//- 将切入点进行注入
@Before(value = "myPointCut()")
public void myBeforeMethod(JoinPoint joinPoint){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 方法参数列表内容
Object[] objs = joinPoint.getArgs();
for(Object obj:objs){
System.out.println(obj);
}
//- 具体的业务逻辑
System.out.println(methodName+"========>>开始执行");
}
@After(value = "myPointCut()")
public void myAfterMethod(JoinPoint joinPoint){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 具体的业务逻辑
System.out.println(methodName+"========>>结束执行");
}
@AfterReturning(value = "myPointCut()",returning ="res")
public void myAfterReturning(JoinPoint joinPoint,Object res){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 具体的业务逻辑
System.out.println(methodName+"========>>返回的数据为:"+res);
}
@AfterThrowing(value = "myPointCut()",throwing ="ex")
public void myAfterThrowing(JoinPoint joinPoint,Throwable ex){
//- 方法名
String methodName = joinPoint.getSignature().getName();
//- 具体的业务逻辑
System.out.println(methodName+"========>>返回的错误信息是:"+ex.getMessage());
}
}
测试类
package com.bboy.demo;
import com.bboy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/22 0022 14:38:14
*/
public class Demo {
public static void main(String[] args) {
//-:获取 spring 容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//-: 从容器中获取Bean对象
UserService userService = (UserService) context.getBean("userService");
userService.updateUser();
userService.deleteUserById(20);
userService.addUser("qs","123456");
}
}