首页 > 其他分享 >Spring AOP

Spring AOP

时间:2022-12-20 22:08:53浏览次数:45  
标签:xiaolyuh Spring AOP org import com annotation 注解


AOP 面向切面编程,相对于OOP面向对象编程。

​​spring​​ AOP存在的目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,且类继承只能是单继承,阻碍更多的行为添加到一组类上,AOP弥补了OOP的不足。

Spring支持AspectJ的注解式切面编程。

1、使用@AspectJ声明一个切面。

2、使用@After、@Before、@Around定义建言(adivice),可以直接将拦截规则(切点)作为参数。

3、其中@After、@Before、@Around参数的拦截规则为切点(PointCut),为了使切点复用,可以使用@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用。

4、其中符合条件的每一个被拦截处为连接点(JoinPoint)。

AspectJ提供了五种定义通知的标注:

​@Before​

  • :前置通知,在调用目标方法之前执行通知定义的任务

​@After​

  • :后置通知,在目标方法执行结束后,无论执行结果如何都执行通知定义的任务

​@After-returning​

  • :后置通知,在目标方法执行结束后,如果执行成功,则执行通知定义的任务

​@After-throwing​

  • :异常通知,如果目标方法执行过程中抛出异常,则执行通知定义的任务

​@Around​

  • :环绕通知,在目标方法执行前和执行后,都需要执行通知定义的任务

本示例将演示基于注解拦截和基于方法规则拦截两种方式,演示一种模拟记录操作的日志系统的实现。其中注解式拦截能够很好的控制要拦截的粒度和获得更丰富的信息,

Spring本身在事务处理(@transcational)和数据缓存(@Cacheable等)上面都使用此种形式拦截。


一、添加AOP支持



[html]  ​​view plain​​  ​​copy​​


 ​​print​


  1. <dependency>  
  2. <groupId>org.springframework</groupId>  
  3. <artifactId>spring-aop</artifactId>  
  4. <version>4.1.6.RELEASE</version>  
  5. </dependency>  
  6. <dependency>  
  7. <groupId>org.aspectj</groupId>  
  8. <artifactId>aspectjrt</artifactId>  
  9. <version>1.8.3</version>  
  10. </dependency>  
  11. <dependency>  
  12. <groupId>org.aspectj</groupId>  
  13. <artifactId>aspectjweaver</artifactId>  
  14. <version>1.8.5</version>  
  15. </dependency>  



二、编写拦截规则的注解


[java]  ​​view plain​​  ​​copy​​


 ​​print​


  1. package com.chenfeng.xiaolyuh.aop.annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.RetentionPolicy;  
  7. import java.lang.annotation.Target;  
  8.   
  9. /**
  10.  * 拦截规则注解
  11.  * 注解本生没有功能,就和XML一样。注解和XML都是一种元数据,元数据即解释数据的数据,这就是所谓的配置。
  12.  * 注解的功能来自用这个注解的地方。
  13.  */  
  14. @Target(ElementType.METHOD)  
  15. @Retention(RetentionPolicy.RUNTIME)  
  16. @Documented  
  17. public @interface Action {  
  18.   
  19.     String name();  
  20. }  



三、编写使用注解的被拦截类


[java]  ​​view plain​​  ​​copy​​


 ​​print​

​​?​

  1. package com.chenfeng.xiaolyuh.aop.service;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. import com.chenfeng.xiaolyuh.aop.annotation.Action;  
  6.   
  7. /**
  8.  * 编写使用注解的被拦截类
  9.  * @ClassName DemoAnnotationService
  10.  * @author yuhao.wang
  11.  * @Date 2017年3月10日 下午3:41:18
  12.  * @version 1.0.0
  13.  */  
  14. @Service  
  15. public class DemoAOPAnnotationService {  
  16.       
  17. @Action(name="编写使用注解的被拦截类")  
  18. public void add() {  
  19. "写使用注解的被拦截类DemoAnnotationService:add");  
  20.     }  
  21. }  



四、编写使用方法规则被拦截的类


[java]  ​​view plain​​  ​​copy​​


 ​​print​

​​?​

  1. package com.chenfeng.xiaolyuh.aop.service;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. /**
  6.  * 编写使用方法规则被拦截类
  7.  * @ClassName DemoMethodService
  8.  * @author yuhao.wang
  9.  * @Date 2017年3月10日 下午3:41:18
  10.  * @version 1.0.0
  11.  */  
  12. @Service  
  13. public class DemoAOPMethodService {  
  14.       
  15. public void add() {  
  16. "使用方法规则被拦截类DemoMethodService:add");  
  17.     }  
  18. }  



五、编写切面


[java]  ​​view plain​​  ​​copy​​


 ​​print​

​​?​

  1. package com.chenfeng.xiaolyuh.aop.aspectj;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.aspectj.lang.JoinPoint;  
  6. import org.aspectj.lang.annotation.After;  
  7. import org.aspectj.lang.annotation.Aspect;  
  8. import org.aspectj.lang.annotation.Before;  
  9. import org.aspectj.lang.annotation.Pointcut;  
  10. import org.aspectj.lang.reflect.MethodSignature;  
  11. import org.springframework.stereotype.Component;  
  12.   
  13. import com.chenfeng.xiaolyuh.aop.annotation.Action;  
  14.   
  15. /**
  16.  * 编写切面
  17.  * @ClassName LogAspect
  18.  * @author yuhao.wang
  19.  * @Date 2017年3月10日 下午3:46:11
  20.  * @version 1.0.0
  21.  */  
  22. @Aspect// 通过@Aspect声明一个切面  
  23. @Component // 通过@Component让此切面成为Spring容器管理的Bean  
  24. public class LogAspect {  
  25. // 通过@Pointcut注解声明一个切点  
  26. @Pointcut("@annotation(com.chenfeng.xiaolyuh.aop.annotation.Action)")   
  27. public void annotationPointCut(){};  
  28.       
  29. // 通过@After注解声明一个建言,并使用@Pointcut定义的切点  
  30. @After("annotationPointCut()")   
  31. public void after(JoinPoint joinPoint) {  
  32. // 通过反射可以获得注解上的属性,然后做日志记录相关的操作  
  33.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();  
  34.         Method method = signature.getMethod();  
  35. class);  
  36. "注解式拦截器:" + action.name());  
  37.     }  
  38.       
  39. // 通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数  
  40. @Before("execution(* com.chenfeng.xiaolyuh.aop.service.DemoAOPMethodService.*(..))")  
  41. public void before(JoinPoint joinpoint) {  
  42.         MethodSignature signature = (MethodSignature) joinpoint.getSignature();  
  43.         Method method = signature.getMethod();  
  44. "拦截规则式拦截器:" + method.getName());  
  45.     }  
  46. }  



六、配置类


[java]  ​​view plain​​  ​​copy​​


 ​​print​


  1. package com.chenfeng.xiaolyuh.aop.config;  
  2.   
  3. import org.springframework.context.annotation.ComponentScan;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.context.annotation.EnableAspectJAutoProxy;  
  6. import org.springframework.context.annotation.FilterType;  
  7.   
  8. import com.chenfeng.xiaolyuh.bean.config.BeanConfig;  
  9.   
  10. @Configuration // 声明当前类是一个配置类,相当于Spring配置的XML文件  
  11. // 包扫描,并排除了对BeanConfig的扫描  
  12. @ComponentScan(basePackages={"com.chenfeng.xiaolyuh"}, excludeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=BeanConfig.class)})  
  13. @EnableAspectJAutoProxy // 使用@EnableAspectJAutoProxy注解开启Spring对AspectJ代理的支持  
  14. public class AopConfig {  
  15.       
  16. }  



七、​​测试​​类


[java]  ​​view plain​​  ​​copy​​


 ​​print​

  1. package com.chenfeng.xiaolyuh.test;  
  2.   
  3. import org.junit.After;  
  4. import org.junit.Test;  
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  6.   
  7. import com.chenfeng.xiaolyuh.aop.config.AopConfig;  
  8. import com.chenfeng.xiaolyuh.aop.service.DemoAOPAnnotationService;  
  9. import com.chenfeng.xiaolyuh.aop.service.DemoAOPMethodService;  
  10.   
  11. /**
  12.  * Created by yuhao.wang on 2017/3/9.
  13.  */  
  14. public class SpringAopTest {  
  15. new AnnotationConfigApplicationContext(AopConfig.class);  
  16.   
  17. @Test  
  18. public void contextTest() {  
  19. class);  
  20. class);  
  21.   
  22.         annotationService.add();  
  23.         methodService.add();  
  24.     }  
  25.   
  26. @After  
  27. public void closeContext() {  
  28.         context.close();  
  29.     }  
  30.   
  31. }  

    参考: https://www.tianmaying.com/tutorial/spring-aop-point-advice




标签:xiaolyuh,Spring,AOP,org,import,com,annotation,注解
From: https://blog.51cto.com/u_15861563/5956815

相关文章

  • Spring Boot「15」统一异常处理
    持续创作,加速成长!这是我参与「掘金日新计划·10月更文挑战」的第15天,点击查看活动详情今天我们将一块学习下SpringMVC中实现统一异常处理的几种方式。总得来说,统一......
  • Spring MVC 拦截器实现登录拦截以及多拦截器的配置执行详解
    持续创作,加速成长!这是我参与「掘金日新计划·10月更文挑战」的第25天,点击查看活动详情前言上一篇文章我们简单了解并完成了SpringMVC拦截器的入门案例,这一篇文章,我们......
  • 构成 Spring Web 服务的各种组件(二)
    6.在客户端上使用SpringWeb服务Spring-WS提供了一个客户端Web服务API,允许对Web服务进行一致的XML驱动访问。它还迎合了编组程序和取消编组程序的使用,以便服务层代码可以......
  • SpringBoot - Yaml语法
    测试用到的类:类的属性必须重写Get与Set方法不管属性是私有的还是公共的,必须重写Get与Set方法@Component@ConfigurationProperties(prefix="student")publicclass......
  • Spring源码编译
    资料参考地址1:Spring源码编译准备环境配置JDK8(与Spring5的兼容性最好)spring:5.2.0release下载Spring源码直接去官方的github库下载,https://github.com/spring......
  • Spring batch
    1.springbatch--批处理框架2.结构:Job>Flow>Step>Chunk>readprocesswrite2.1基本概念:SpringBatch运行基本单位是一个job,一个job就做一件批处理事情。......
  • SpringBoot - @ImportResource,@ConfigurationProperties 让xml生效与类属性绑定配置文
    @ImportResource作用:使用.xml配置文件范围:必须使用在主程序@SpringBootApplication或配置类上@Configuration@SpringBootApplication@ImportResource("classpath:appl......
  • SpringBoot - 条件注解 @Conditional
    @ConditiOnBean作用:如果Spring容器里面存在指定的Bean则生效范围:类上,方法上,一般在配置类中使用参数:value参数类型Class[],name参数类型String[]IOC容器中组件的名称......
  • (1)SpringMVC前传
    在我们熟知的建立在三层结构(表示层、业务逻辑层、持久层)基础之上的J2EE应用程序开发之中,表示层的解决方案最多。因为在表示层自身的知识触角很多,需要解决的问题也不少,这也就......
  • SpringBoot - 配置包扫描注解@ComponentScan
    @ComponentScan作用:配置包扫描规则范围:主程序类上(被@SpringBootApplication修饰),或配置类上(被@Configuration修饰)参数:value指定要扫描的包,excludeFilters配置排除......