4、AOP

[复制链接]
发表于 2023-3-18 10:44:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1、AOP

1.1什么是AOP

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

1.2、Aop在Spring中的作用

提供声明式事务:允许用户自定义切面

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

在SpringAop中,通过Advice定义横切逻辑,Spring中支持5中类型的Advice:

即Aop在不改变原有代码的情况虚啊,去增加新的功能。
1.3、使用Spring实现Aop

【重点】使用aop切入,需要导入一个依赖包
  1. <dependency>
  2.     <groupId>org.aspectj</groupId>
  3.     <artifactId>aspectjweaver</artifactId>
  4.     <version>1.9.4</version>
  5. </dependency>
复制代码
注意:依赖包下不要有那个runtime的scope,有就在导入包前把它去掉;若没去掉导入了包,用ctrl+shift+alt+s 把依赖包设置成Compile
方式一:使用Spring的API接口

  • 接口
  1. package com.jan.service;
  2. public interface UserService {
  3.     public void add();
  4.     public void delete();
  5.     public void update();
  6.     public void select();
  7. }
复制代码

  • 实现类
  1. package com.jan.service;
  2. public class UserServiceImpl implements UserService{
  3.     public void add() {
  4.         System.out.println("增加了一个用户");
  5.     }
  6.     public void delete() {
  7.         System.out.println("删除了一个用户");
  8.     }
  9.     public void update() {
  10.         System.out.println("更行了一个用户");
  11.     }
  12.     public void select() {
  13.         System.out.println("查询了一个用户");
  14.     }
  15. }
复制代码

  • 日记类
  1. package com.jan.log;
  2. import org.springframework.aop.MethodBeforeAdvice;
  3. import java.lang.reflect.Method;
  4. public class Log  implements MethodBeforeAdvice {
  5.     //method:要执行的目标对象的方法
  6.     //args: 参数
  7.     //target: 目标对象
  8.     public void before(Method method, Object[] args, Object target) throws Throwable {
  9.         System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
  10.     }
  11. }
复制代码
  1. package com.jan.log;
  2. import org.springframework.aop.AfterReturningAdvice;
  3. import java.lang.reflect.Method;
  4. public class AfterLog implements AfterReturningAdvice {
  5.     //returnValue ;返回值
  6.     public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
  7.         System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
  8.     }
  9. }
复制代码

  • 测试类
  1. import com.jan.service.UserService;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyText {
  5.     public static void main(String[] args) {
  6.     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext");
  7.     //动态代理 代理的是接口!
  8.         UserService userService = context.getBean("userService", UserService.class);
  9.         userService.add();
  10.     }
  11. }
复制代码

  • bean
  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.       
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.       
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.         https://www.springframework.org/schema/beans/spring-beans.xsd
  9.         
  10.         http://www.springframework.org/schema/aop
  11.         https://www.springframework.org/schema/aop/spring-aop.xsd">
  12.      
  13.     <bean id="userService" />
  14.     <bean id="afterLog" />
  15.     <bean id="log" />
  16.     <aop:config>
  17.         
  18.         <aop:pointcut id="pointcut" expression="execution(* com.jan.service.UserServiceImpl.*(..))"/>
  19.         <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
  20.         <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  21.     </aop:config>
  22. </beans>
复制代码
没有切面,只需要定义一个类,去实现它的接口,可以实现多个 类文件的切入,功能比较强大
方法二:自定义来实现AOP
  1. package com.jan.custom;
  2. public class CustomPointCut {
  3.     public void before(){
  4.         System.out.println("===========方法执行前================");
  5.     }
  6.     public void after(){
  7.         System.out.println("===========方法执行后================");
  8.     }
  9. }
复制代码

  • bean
  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.       
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.       
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.         https://www.springframework.org/schema/beans/spring-beans.xsd
  9.         
  10.         http://www.springframework.org/schema/aop
  11.         https://www.springframework.org/schema/aop/spring-aop.xsd">
  12.      
  13.     <bean id="userService" />
  14.     <bean id="afterLog" />
  15.     <bean id="log" />
  16.     <aop:config>
  17.         
  18.         <aop:pointcut id="pointcut" expression="execution(* com.jan.service.UserServiceImpl.*(..))"/>
  19.         <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
  20.         <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  21.     </aop:config>
  22. </beans><?xml version="1.0" encoding="UTF-8"?>
  23. <beans xmlns="http://www.springframework.org/schema/beans"
  24.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.       
  26.        xmlns:aop="http://www.springframework.org/schema/aop"
  27.       
  28.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  29.         https://www.springframework.org/schema/beans/spring-beans.xsd
  30.         
  31.         http://www.springframework.org/schema/aop
  32.         https://www.springframework.org/schema/aop/spring-aop.xsd">
  33.      
  34.     <bean id="userService" />
  35.     <bean id="afterLog" />
  36.     <bean id="log" />
  37.     <aop:config>
  38.         
  39.         <aop:pointcut id="pointcut" expression="execution(* com.jan.service.UserServiceImpl.*(..))"/>
  40.         <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
  41.         <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  42.     </aop:config>
  43. </beans><?xml version="1.0" encoding="UTF-8"?>
  44. <beans xmlns="http://www.springframework.org/schema/beans"
  45.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  46.        xmlns:aop="http://www.springframework.org/schema/aop"
  47.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  48.         https://www.springframework.org/schema/beans/spring-beans.xsd
  49.         http://www.springframework.org/schema/aop
  50.         https://www.springframework.org/schema/aop/spring-aop.xsd">
  51.      
  52.     <bean id="userService" />
  53.     <bean id="afterLog" />
  54.     <bean id="log" />
  55.     <aop:aspectj-autoproxy/>
  56.     <aop:aspectj-autoproxy proxy-target-/>
  57.     <bean id="annotationPointCut" />
  58. </beans>  
复制代码
自定义类更加明确了,自定义切面,切入点,再到通知(什么时候执行)
方法三:使用注解实现
  1. package com.jan.custom;
  2. //方式三:使用注解方式实现AOP
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.After;
  5. import org.aspectj.lang.annotation.Around;
  6. import org.aspectj.lang.annotation.Aspect;
  7. import org.aspectj.lang.annotation.Before;
  8. @Aspect
  9. public class AnnotationPointCut {
  10.     @Before("execution(* com.jan.service.UserServiceImpl.*(..))")
  11.     public void before(){
  12.         System.out.println("=====方法执行前========");
  13.     }
  14.     @After("execution(* com.jan.service.UserServiceImpl.*(..))")
  15.     public void after(){
  16.         System.out.println("=====方法执行后========");
  17.     }
  18.     @Around("execution(* com.jan.service.UserServiceImpl.*(..))")
  19.     public void around(ProceedingJoinPoint jp) throws Throwable {
  20.         System.out.println("环绕前");
  21.         Object proceed = jp.proceed();//执行方法
  22.         System.out.println("环绕后");
  23.     }
  24. }
复制代码

  • bean
  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.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.         https://www.springframework.org/schema/beans/spring-beans.xsd
  7.         http://www.springframework.org/schema/aop
  8.         https://www.springframework.org/schema/aop/spring-aop.xsd">
  9.      
  10.     <bean id="userService" />
  11.     <bean id="afterLog" />
  12.     <bean id="log" />
  13.     <aop:aspectj-autoproxy/>
  14.     <aop:aspectj-autoproxy proxy-target-/>
  15.     <bean id="annotationPointCut" />
  16. </beans>
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表