Spring~使用三种方式在Spring中实现AOP (使用Spring注解、使用自定义切面、使用aspectj注解)

文章介绍了如何在Spring框架中使用AOP(面向切面编程)实现日志记录功能,包括创建AfterReturningAdvice实现类,配置切点表达式、切入点和通知,以及在XML中使用AspectJ注解进行配置。
摘要由CSDN通过智能技术生成

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will be a loser.

  • User: Listen-Y.

  • Date: 2020-10-23

  • Time: 21:46

*/

public class LogAfter implements AfterReturningAdvice {

@Override

public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {

System.out.println(“方法名是:” + method.getName() + " 返回值是:" +

o);

}

}

  1. 注册上述所有类的bean
  1. 在xml文件中配置AOP, 配置切入点, 配置切入点与通知的连接
<?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: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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

aop:config

<aop:pointcut id=“pointcut” expression=“execution(* aop.Service.*(…))”/>

<aop:advisor advice-ref=“logBefore” pointcut-ref=“pointcut”/>

<aop:advisor advice-ref=“logAfter” pointcut-ref=“pointcut”/>

</aop:config>

  1. 测试

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);

Database database = context.getBean(“service”, Database.class);

database.add();

}

}

在这里插入图片描述

总结就是: 导包, 写实体类, 配置xml

使用自定义切面实现AOP


  1. 导包和上面的依赖是一样的

  2. 实现切面实体

public class Log {

public void before() {

System.out.println(“=method begin=”);

}

public void after() {

System.out.println(“=method end=”);

}

}

  1. 配置xml文件
  • 主要是配置切面、切入点和配置连接通知

  • 要有需要被代理的类和切面的类的bean对象

  • 在xml文件中配置aop, 与上面的不同的是, 这里需要配置切面, 然后配置切入点, 最后配置通知与切入点的连接

<?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: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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

aop:config

<aop:aspect ref=“log”>

<aop:pointcut id=“pointcut” expression=“execution(* myProxy.Dao.*(…))”/>

<aop:before method=“before” pointcut-ref=“pointcut”/>

<aop:after method=“after” pointcut-ref=“pointcut”/>

</aop:aspect>

</aop:config>

  1. 测试

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);

Database database = context.getBean(“dao”, Database.class);

database.add();

}

}

在这里插入图片描述

总结就是: 导包, 编写实体类, 但是是直接写切面, 然后在xml文件中配置切面和配置通知和切入点的连接, 使用这个方法最重要的就是配置xml

使用aspectj注解实现AOP


  1. 导入aspect的包, 还和上面的一样

  2. 给切面增注解@Aspect

  3. 给切面中的通知添加注解, 实现什么时候执行, 注解的后面还要配置切入点, 在环绕增强中around, 我们可以给定一个参数,去代表我们要执行的切入点

package annotationProxy;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will be a loser.

  • User: Listen-Y.

  • Date: 2020-10-24

  • Time: 13:58

*/

//添加注解表示这是一个切面

@Aspect

public class Log {

//添加注解, 配置切入点

@Before(“execution(* myProxy.Dao.*(…))”)

public void before() throws Throwable {

System.out.println(“=方法执行前=”);

}

@After(“execution(* myProxy.Dao.*(…))”)

public void after() {

System.out.println(“=方法执行后=”);

}

//环绕,使用这个注解可以给定一个参数, 去代表我们的切入点

@Around(“execution(* myProxy.Dao.*(…))”)

public void around(ProceedingJoinPoint joinPoint) throws Throwable {

System.out.println(“环绕前”);

//这个代表去执行这个方法

Object proceed = joinPoint.proceed();

System.out.println(“环绕后”);

}

}

  1. 配置xml文件, 开启aop的注解扫描

  2. 将被代理类和切面添加到容器中

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料


《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
、源码讲义、实战项目、讲解视频,并且会持续更新!**

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料

[外链图片转存中…(img-IcG8HZ4A-1711858115797)]
[外链图片转存中…(img-oww6Mj1f-1711858115797)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值