Spring知识小汇(7)——AOP的相关知识
发布日期:2021-05-07 08:59:27 浏览次数:22 分类:精选文章

本文共 3244 字,大约阅读时间需要 10 分钟。

文章目录

AOP

什么是AOP

  • AOP:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架的一个重要内容,是函数式编程的一种衍生。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率

AOP在Spring中的作用

  1. 提供声明式事务;允许用户自定义切面
  2. 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等
  3. 切面(aspect):横切关注点被模块化的特殊对象。即,它是一个类
  4. 通知(advice):切面必须完成的工作。即,它是类中的一个方法
  5. 目标(target):被通知对象
  6. 代理(proxy):向目标对象应用通知之后创建的对象
  7. 切入点(pointCut):切面通知执行的“地点”的定义
  8. 连接点(joinPoint):与切入点匹配的执行点

使用spring实现AOP

导入的依赖

org.aspectj
aspectjweaver
1.9.6

AOP的实现

方式一:使用spring原生API接口【主要是spring中的API接口】

applicationContext.xml

UserService

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

UserServiceImpl

@Servicepublic class UserServiceImpl implements UserService {       public void add() {           System.out.println("增加一个方法");    }    public void delete() {           System.out.println("删除一个方法");    }    public void update() {           System.out.println("更新一个方法");    }    public void search() {           System.out.println("查找一个方法");    }}

AfterLog

@Componentpublic class AfterLog implements AfterReturningAdvice {                              //返回值类型   要执行目标对象的方法  执行参数        目标对象    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {           System.out.println("执行的方法为" + method.getName() + ",返回的结果为" + o);    }}

BeforeLog

@Componentpublic class BeforeLog implements MethodBeforeAdvice {                         //要执行的目标对象的方法   执行的参数   目标对象    public void before(Method method, Object[] objects, Object o) throws Throwable {           System.out.println(method.getClass().getName() + "的" + method.getName() + "被执行了");    }}

test

public class MyTest {       public static void main(String[] args) {           ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        UserService userService = (UserService) context.getBean("userServiceImpl");        userService.add();    }}
方式二:自定义来实现AOP【切面】

applicationContext.xml【主要配置】

自定义方法

@Componentpublic class DiyMethod {       public void before() {           System.out.println("before...");    }    public void after() {           System.out.println("after...");    }}
方式三:使用注解实现

applicationContext.xml【主要配置】

定义注解的类

@Component@Aspect//表示是一个切面public class AnnoMethod {       @Before("execution(* com.wjq.service.UserServiceImpl.*(..))")    public void before() {           System.out.println("---before---");    }    @After("execution(* com.wjq.service.UserServiceImpl.*(..))")    public void after() {           System.out.println("---after---");    }    //环绕增加  要有一个参数,代表我们的切入点    @Around("execution(* com.wjq.service.UserServiceImpl.*(..))")    public void around(ProceedingJoinPoint pj) throws Throwable {           System.out.println("环绕前");        Signature signature = pj.getSignature();        System.out.println(signature);        Object proceed = pj.proceed();//执行方法        System.out.println("环绕后");    }}

在这里插入图片描述

上一篇:echarts的基本使用
下一篇:element 下拉框常见问题

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月13日 03时10分11秒