ToB企服应用市场:ToB评测及商务社交产业平台

标题: AOP面向切面编程简单介绍 [打印本页]

作者: 海哥    时间: 2022-9-16 17:24
标题: AOP面向切面编程简单介绍
AOP面向切面编程

什么是AOP

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

提供声明式事务;允许用户自定义切面
AOP 主要就是在不改变原本代码的前提下,新增功能上去不影响原本的功能。
AOP在Spring中是非常重要的一个功能,可以理解为一个业务就是一条线,当使用一把刀在这条线的指定位置砍下去,添加新的功能到断开处,最后在进行织入,最后连起来成为了一条新的线,新的功能就可以实现。
使用Spring实现Aop

添加依赖
  1. <dependency>
  2.     <groupId>org.springframework</groupId>
  3.     <artifactId>spring-aop</artifactId>
  4.     <version>5.3.18</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.aspectj</groupId>
  8.     <artifactId>aspectjrt</artifactId>
  9.     <version>1.9.6</version>
  10.     <scope>runtime</scope>
  11. </dependency>
  12. <dependency>
  13.     <groupId>org.springframework</groupId>
  14.     <artifactId>spring-aspects</artifactId>
  15.     <version>5.3.18</version>
  16. </dependency>
复制代码
注解实现

修改配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:aop="http://www.springframework.org/schema/aop"
  5.        xmlns:context="http://www.springframework.org/schema/context"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.          http://www.springframework.org/schema/beans/spring-beans.xsd
  8.          http://www.springframework.org/schema/context
  9.          http://www.springframework.org/schema/context/spring-context.xsd
  10.          http://www.springframework.org/schema/aop
  11.          http://www.springframework.org/schema/aop/spring-aop.xs">
  12.    
  13.     <context:component-scan base-package="com.bing"/>
  14.    
  15.     <aop:aspectj-autoproxy />
  16. </beans>
复制代码
添加一个切面类
  1. package com.bing.aspect;
  2. import org.apache.log4j.Logger;
  3. import org.aspectj.lang.JoinPoint;
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5. import org.aspectj.lang.annotation.*;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * @Author IBing
  9. * @Date 2022/8/26 22:01
  10. * @Version 1.0
  11. */
  12. @Aspect  //表示这是一个 切面类
  13. @Component  //将对象放入spring容器
  14. public class UserAspect {
  15.         //开启日志
  16.     private Logger logger = Logger.getLogger(UserAspect.class);
  17.     /**
  18.      * 目标方法执行之前执行
  19.      * @param joinPoint
  20.      */
  21.     @Before("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
  22.     public void before(JoinPoint joinPoint){
  23.         logger.debug("before");
  24.         logger.debug("拦截的目标对象"+joinPoint.getTarget());
  25.         logger.debug("拦截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
  26.         logger.debug("拦截的参数"+joinPoint.getArgs());
  27.         logger.debug("拦截的位置"+joinPoint.getStaticPart());
  28.         logger.debug("拦截的代理对象"+joinPoint.getStaticPart());
  29.     }
  30.     /**
  31.      * 无论是否抛出异常都会执行,相当于finally
  32.      * @param joinPoint
  33.      */
  34.     @After("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
  35.     public void after(JoinPoint joinPoint){
  36.         logger.debug("after");
  37.         logger.debug("拦截的目标对象"+joinPoint.getTarget());
  38.         logger.debug("拦截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
  39.         logger.debug("拦截的参数"+joinPoint.getArgs());
  40.         logger.debug("拦截的位置"+joinPoint.getStaticPart());
  41.         logger.debug("拦截的代理对象"+joinPoint.getStaticPart());
  42.     }
  43.     /**
  44.      * 相当于try
  45.      * @param joinPoint
  46.      */
  47.     @AfterReturning("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
  48.     public void afterReturning(JoinPoint joinPoint){
  49.         logger.debug("afterReturning");
  50.         logger.debug("拦截的目标对象"+joinPoint.getTarget());
  51.         logger.debug("拦截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
  52.         logger.debug("拦截的参数"+joinPoint.getArgs());
  53.         logger.debug("拦截的位置"+joinPoint.getStaticPart());
  54.         logger.debug("拦截的代理对象"+joinPoint.getStaticPart());
  55.     }
  56.     /**
  57.      * 相当于catch
  58.      * @param joinPoint
  59.      * @param e
  60.      */
  61.     @AfterThrowing(value="execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))",throwing ="e" )
  62.     public void afterThrowing(JoinPoint joinPoint,RuntimeException e){
  63.         logger.debug("afterThrowing");
  64.         logger.debug("拦截的目标对象"+joinPoint.getTarget());
  65.         logger.debug("抛出的异常为",e);
  66.     }
  67.     /**
  68.      * 环绕执行
  69.      * @param pjd
  70.      * @return
  71.      * @throws Throwable
  72.      */
  73.     @Around("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
  74.     public Object around(ProceedingJoinPoint pjd) throws Throwable {
  75.         logger.debug("around之前");
  76.         Object proceed= pjd.proceed();
  77.         logger.debug("around之后");
  78.         return proceed;
  79.     }
  80. }
复制代码
测试
  1. @Test
  2. public void test( ){
  3.     //创建spring的容器并初始化
  4.     ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
  5.     String[] beanNameForType = classPathXmlApplicationContext.getBeanNamesForType(UserController.class);
  6.     System.out.println(Arrays.toString(beanNameForType));
  7.     UserController bean = (UserController) classPathXmlApplicationContext.getBean("userController");
  8.     bean.login("aa","123"); //这是 UserController 里写好的login方法
  9.     classPathXmlApplicationContext.close();
  10.     }
复制代码
输出结果

因为没有异常,所以 **afterThrowing **方法没有执行
下面是一些切入点语法
  1. 直接精准到一个方法上面去
  2. execution( public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
  3. 任意权限修饰符
  4. execution(  com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
  5. 无返回类型
  6. execution( void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
  7. 有返回类型
  8. execution( !void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
  9. 任意返回类型
  10. execution( * com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
  11. 任意参数
  12. execution( * com.bing.service.impl.UserServiceImpl.login(..))
  13. 类中的任意方法
  14. execution( * com.bing.service.impl.UserServiceImpl.*(..))
  15. 类中以指定内容开头的方法
  16. execution( * com.bing.service.impl.UserServiceImpl.select*(..))
  17. 包中的任意类的任意方法不包含子包下面的类
  18. execution( * com.bing.service.impl.*.*(..))
  19. 包中及其下的任意类的任意方法
  20. execution( * com.bing.service..*.*(..))
复制代码
AOP还可以用XML方法实现,这里就不演示了,如果大家有兴趣,可以自行百度实现

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4