spring-AOP实现方式

1 名词

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ....
  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy):向目标对象应用通知之后创建的对象。
  • 切入点(PointCut):切面通知 执行的 “地点”的定义。
  • 连接点(JointPoint):与切入点匹配的执行点
  • 引入(Introduction) : 声明某个方法或字段。
  • AOP 代理(AOp Proxy) : AOP框架创建的对象用来实现切面。
  • 织入(Weaving) : 将通知应用到切入点的过程。

2 准备工作

2.1 引入依赖

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>

2.2 在spring配置文件中添加AOP约束

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd

2.3 创建抽象角色

public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}

2.4 创建真实角色

public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void select() {
System.out.println("查询了一个用户");
}
}

2.5 在spring配置文件中注册真实角色Bean

<bean id="userService" class="com.lv.service.UserServiceImpl"/>

3 AOP实现方式一:使用原生的spring API接口

3.1 编写两个增强类

public class Log implements MethodBeforeAdvice {
/**
*
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法");
}
}
public class AfterLog implements AfterReturningAdvice {
/**
*
* @param returnValue 返回值
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method+"方法,返回结果为:"+returnValue);
}
}

3.2 在spring配置文件中注册Bean

<bean id="log" class="com.lv.log.Log"/>
<bean id="afterLog" class="com.lv.log.AfterLog"/>

3.3 在spring配置文件中配置AOP

<!--配置AOP:需要导入AOP的约束-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置! * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.lv.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加!-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

3.4 测试

public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理,代理的是接口!
UserService userService = (UserService) context.getBean("userService");
userService.select();
}
}

3.5 执行结果

4 AOP实现方式二:自定义类实现

4.1 编写一个切入类

public class DIyPointCut {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}

4.2 在spring配置文件中注册切入类

<bean id="diy" class="com.lv.diy.DIyPointCut"/>

4.3 在spring配置文件中配置AOP

<aop:config>
<!--自定义切面 ref 要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.lv.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>

4.4 测试代码不变,和上面的相同

4.5 执行结果

5 AOP实现方式三:注解方式实现

5.1编写一个注解实现的增强类

@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.lv.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=========方法执行前=========");
}
@After("execution(* com.lv.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=========方法执行后==========");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.lv.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Signature signature = jp.getSignature();//获取签名
System.out.println("signature:"+signature);
//执行方法
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}

5.2 在spring配置文件中注册注解增强类的Bean

<bean id="annotationPointCut" class="com.lv.diy.AnnotationPointCut"/>

5.3 在spring配置文件中开启注解支持

<!--开启注解支持! 底层两种实现方式 JDK(默认,不写就是这个):proxy-target-class="false" cglib:proxy-target-class="true"-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

5.4 测试代码不变

5.5 执行结果

相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示