SSM 您所在的位置:网站首页 5800x核心电压15v SSM

SSM

2023-10-22 03:49| 来源: 网络整理| 查看: 265

前言AOP在Spring中的作用Spring实现AOP1、方式一:Spring的API接口!2、方法二:自定义类实现AOP3、方法三:注解实现AOP

前言

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

AOP在Spring中的作用

提供申明式服务:允许用户自定义切面

了解以下几个专有名词

横切关注点 :跨越应用程序多个模块的方法或功能。即使与我们的业务逻辑无关,但是我们需要关注的部分,就是横切关注点! Eg: 日志,安全,缓存等切面(Aspect) : 横切关注点,被模块化的特殊对象 即 类 (日志类,安全类等)通知(Adive) : 切面必须要完成的工作 即 类的方法 (日志类的方法,安全类的方法)目标(Target) : 被通知对象代理(Proxy) : 向目标对象应用通知之后创建的对象切入点(PointCut) :切面通知执行的“地点”的定义连接点(JointPoint) : 与切入点匹配的执行点

SpringAOP中,通过advice定义横切逻辑,Spring支持的5中类型的Advice

通知类型连接点实现接口前置通知方法前org.springframework.aop.MethodBeforeAdvice后置通知方法后org.springframework.aop.AfterReturningAdvice环绕通知方法前后org.springframework.aop.MethodInterceptor异常抛出通知方法抛出异常org.springframework.aop.ThrowsAdvice引介通知类中增加新的方法属性org.springframework.aop.IntroductionInterceptor Spring实现AOP

使用AOP织入,需要导入一个依赖包!

org.springframework spring-webmvc 5.2.13.RELEASE org.springframework spring-jdbc 5.2.13.RELEASE org.springframework spring-aspects 5.2.2.RELEASE junit junit 4.12 test org.aspectj aspectjweaver 1.9.4 1、方式一:Spring的API接口!

案例,打印日志!

注意,在我们配置之后,工具会给我们提示,即代码左边的标志!

UserService接口!

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

在这里插入图片描述

UserServiceImpl

public class UserServiceImpl implements UserService { public void add() { System.out.println("增加了一个用户信息"); } public void delete() { System.out.println("删除了一个用户信息"); } public void query() { System.out.println("查询了一个用户信息"); } public void update() { System.out.println("更新了一个用户信息"); } }

在这里插入图片描述

前置日志 实现MethodBeforeAdvice接口

public class PrevLog implements MethodBeforeAdvice { /* * @param method 要执行目标对象的方法 * @param objects 参数 * @param o 目标对象 * @return void * @author 码农天宇 * @date 2021/4/5 10:02 */ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("[PrevLog: ]" + new SimpleDateFormat("yyyy-MM-hh hh:mm:ss").format(new Date()) + o.getClass().getName() + "的" + method.getName() + "被执行了"); } }

在这里插入图片描述

后置日志 实现AfterReturningAdvice接口

public class AfterLog implements AfterReturningAdvice { /* * @param o 返回结果 * @param method 要执行目标对象的方法 * @param objects 参数 * @param o1 目标对象 * @return void * @author 码农天宇 * @date 2021/4/5 10:47 */ public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("[AfterLog: ]" + new SimpleDateFormat("yyyy-MM-hh hh:mm:ss").format(new Date()) + "执行了" + method.getName() + "返回结果为" + o); } }

在这里插入图片描述

配置bean,我们用原生的配置方式! 这里我们要引入aop约束!

在这里插入图片描述

测试!

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

结果!

[PrevLog: ]2021-04-10 10:45:27com.tian.aop.demo01.service.UserServiceImpl的add被执行了 增加了一个用户信息 [AfterLog: ]2021-04-10 10:45:27执行了add返回结果为null [PrevLog: ]2021-04-10 10:45:27com.tian.aop.demo01.service.UserServiceImpl的delete被执行了 删除了一个用户信息 [AfterLog: ]2021-04-10 10:45:27执行了delete返回结果为null [PrevLog: ]2021-04-10 10:45:27com.tian.aop.demo01.service.UserServiceImpl的query被执行了 查询了一个用户信息 [AfterLog: ]2021-04-10 10:45:27执行了query返回结果为null [PrevLog: ]2021-04-10 10:45:27com.tian.aop.demo01.service.UserServiceImpl的update被执行了 更新了一个用户信息 [AfterLog: ]2021-04-10 10:45:27执行了update返回结果为null

在这里插入图片描述

2、方法二:自定义类实现AOP

diy类

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

配置bean

测试!

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

结果!

方法执行前 增加了一个用户信息 方法执行后 方法执行前 删除了一个用户信息 方法执行后 方法执行前 查询了一个用户信息 方法执行后 方法执行前 更新了一个用户信息 方法执行后

3、方法三:注解实现AOP

注解实现

@Aspect // 标志这个类是一个切面 public class AnnotionPointCut { @Before("execution(* com.tian.aop.demo01interface.service.UserServiceImpl.*(..)))") public void before(){ System.out.println("方法执行前"); } @After("execution(* com.tian.aop.demo01interface.service.UserServiceImpl.*(..)))") public void after(){ System.out.println("方法使用后"); } @Around("execution(* com.tian.aop.demo01interface.service.UserServiceImpl.*(..)))") public void arount(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕前"); // 打印签名 即 执行的方法! System.out.println(proceedingJoinPoint.getSignature()); // 执行方法 Object proceed = proceedingJoinPoint.proceed(); System.out.println("环绕后"); } }

配置bean

结果!

环绕前 void com.tian.aop.demo01interface.service.UserServiceImpl.add() 方法执行前 增加了一个用户信息 方法使用后 环绕后 环绕前 void com.tian.aop.demo01interface.service.UserServiceImpl.delete() 方法执行前 删除了一个用户信息 方法使用后 环绕后



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有