SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

AOP(Aspect Oriented Programming),是面向切面编程的技术。AOP基于IoC基础,是对OOP的有益补充。


    AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分:核心业务逻辑(Core business concerns)及横向的通用逻辑,也就是所谓的切面Crosscutting enterprise concerns。例如,所有大中型应用都要涉及到的持久化管理(Persistent)、事务管理(Transaction Management)、权限管理(Privilege Management)、日志管理(Logging)和调试管理(Debugging)等。

      

    使用AOP技术,可以让开发人员只专注核心业务,而通用逻辑则使用AOP技术进行横向切入,由专人去处理这些通用逻辑,会使得任务简单明了,提高开发和调试的效率。

   

    使用AOP,我们要注意关注横切性的功能,即抽象出独立服务,进行模块化使我们以前习惯性的纵向思维的方法再改变,注意横向思考问题的方式,我们结合现在的系统可以把判断文本框一些了的验证、日志的记录、事务的开启、数据库的开启和关闭等等,都可以抽象出使用切面的形式把这些方法切入进去,我们只需要关心我们的业务逻辑,这样代码简单,间接,开发效率大大提高,更重要的是复用效率大大提高了。 


基本概念

       要想了解AOP,首先得了解几个重要的基本概念:

《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》


  • 切面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。比如说事务管理就是J2EE应用中一个很好的横切关注点例子。切面用Spring的Advisor或拦截器实现。
  • 连接点(Joinpoint):程序执行过程中明确的点,如方法的调用或特定的异常被抛出。
  • 通知(Advice):在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。
  • 切入点(Pointcut):指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点,例如,使用正则表达式。
  • 目标对象(Target Object):包含连接点的对象,也被称作被通知或被代理对象。
  • AOP代理(AOP Proxy):AOP框架创建的对象,包含通知。在Spring中,AOP代理可以是JDK动态代理或CGLIB代理。
  • 编织(Weaving):组装方面来创建一个被通知对象。这可以在编译时完成(例如使用AspectJ编译器),也可以在运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。


各种通知(Advice)类型

    为了符合各种流程处理,通知类型提供了5种,可以对目标方法进行全方位处理:

  • Before advice:在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。
    ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。
  • After advice:当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
    ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。
  • After returnadvice:在某连接点正常完成后执行的通知,不包括抛出异常的情况。
    ApplicationContext中在<aop:aspect>里面使用<aop:after-returning>元素进行声明。
  • Around advice:包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。
    ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。
  • Afterthrowing advice:在方法抛出异常退出时执行的通知。
    ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。

本文采用的JDK代理方式


AspectJ注解方式配置AOP,实例讲解


SecurityHandler,这个通知类可以换成安全性检测、日志管理、事务开启关闭等等。


[java]
view plain
copy
print
?
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》

  1. <span style=“font-family:FangSong_GB2312;font-size:18px;”>package com.bjpowernode.spring;  
  2.   
  3. import org.aspectj.lang.annotation.After;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.Before;  
  6. import org.aspectj.lang.annotation.Pointcut;  
  7.   
  8. @Aspect  
  9. public class SecurityHandler {  
  10.       
  11.       
  12.     /** 
  13.      * 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数 
  14.      * 该方法就是一个标识,不进行调用 
  15.      */  
  16.     @Pointcut(“execution(* add*(..))”)  
  17.     private void addAddMethod(){};  
  18.       
  19.       
  20.     /** 
  21.      * 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上 
  22.      */  
  23.     //@Before(“addAddMethod()”)  
  24.     @After(“addAddMethod()”)  
  25.     private void checkSecurity() {  
  26.         System.out.println(“——-checkSecurity——-“);  
  27.     }         
  28. }  
  29. </span>  


UserManager接口



[java]
view plain
copy
print
?
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》

  1. <span style=“font-family:FangSong_GB2312;font-size:18px;”>package com.bjpowernode.spring;  
  2.   
  3. public interface UserManager {  
  4.   
  5.     public void addUser(String username, String password);  
  6.       
  7.     public void delUser(int userId);  
  8.       
  9.     public String findUserById(int userId);  
  10.       
  11.     public void modifyUser(int userId, String username, String password);  
  12. }  
  13. </span>  


UserManagerImpl实现



[java]
view plain
copy
print
?
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》

  1. <span style=“font-family:FangSong_GB2312;font-size:18px;”>package com.bjpowernode.spring;  
  2.   
  3. public class UserManagerImpl implements UserManager {  
  4.   
  5.     public void addUser(String username, String password) {  
  6.         //checkSecurity();  
  7.         System.out.println(“———UserManagerImpl.add()——–“);  
  8.     }  
  9.   
  10.     public void delUser(int userId) {  
  11.         //checkSecurity();  
  12.         System.out.println(“———UserManagerImpl.delUser()——–“);  
  13.     }  
  14.   
  15.     public String findUserById(int userId) {  
  16.         //checkSecurity();  
  17.         System.out.println(“———UserManagerImpl.findUserById()——–“);  
  18.         return “张三”;  
  19.     }  
  20.   
  21.     public void modifyUser(int userId, String username, String password) {  
  22.         //checkSecurity();  
  23.         System.out.println(“———UserManagerImpl.modifyUser()——–“);  
  24.     }  
  25.   
  26. //  private void checkSecurity() {  
  27. //      System.out.println(“——-checkSecurity——-“);  
  28. //  }  
  29. }  
  30. </span>  


application-config.xml中,只需要配置业务逻辑bean和Aspect bean,并启用Aspect注解即可:


[html]
view plain
copy
print
?
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》

  1. <span style=“font-family:FangSong_GB2312;font-size:18px;”><?xml version=“1.0” encoding=“UTF-8”?>  
  2.   
  3. <beans xmlns=“http://www.springframework.org/schema/beans”  
  4.          xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  5.          xmlns:aop=“http://www.springframework.org/schema/aop”  
  6.          xmlns:tx=“http://www.springframework.org/schema/tx”  
  7.          xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd”>  
  10.              
  11.     <!– 启用AspectJ对Annotation的支持 –>         
  12.     <aop:aspectj-autoproxy/>             
  13.       
  14.     <bean id=“userManager” class=“com.bjpowernode.spring.UserManagerImpl”/>  
  15.       
  16.     <bean id=“securityHandler” class=“com.bjpowernode.spring.SecurityHandler”/>  
  17. </beans>  
  18. </span>  


Client客户端


[java]
view plain
copy
print
?
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》

  1. <span style=“font-family:FangSong_GB2312;font-size:18px;”>package com.bjpowernode.spring;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Client {  
  7.   
  8.     public static void main(String[] args) {  
  9.         BeanFactory factory = new ClassPathXmlApplicationContext(“applicationContext.xml”);  
  10.         UserManager userManager = (UserManager)factory.getBean(“userManager”);  
  11.         userManager.addUser(“张三”“123”);  
  12.     }  
  13.   
  14. }  
  15. </span>  

效果图


《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》


当然我们通过注解的方式可以实现灵活的配置


[java]
view plain
copy
print
?
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》
《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》

  1. <span style=“font-family:FangSong_GB2312;font-size:18px;”>/** 
  2.      * 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上 
  3.      */  
  4.     @Before(“addAddMethod()”)  
  5.     //@After(“addAddMethod()”)  
  6.     private void checkSecurity() {  
  7.         System.out.println(“——-checkSecurity——-“);  
  8.     }       </span>  


修改后的效果:


《SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP》


总结

 Annotation

    优点

      保存在 class 文件中,降低维护成本。

          无需工具支持,无需解析。

          编译期即可验证正确性,查错变得容易。 

   缺点
        
若要对配置项进行修改,不得不修改 Java 文件,重新编译打包应用。

     总的来说,AOP这样的好处是我们抽象出公共的方法之后,我们的代码,方法复用性大大提高,实现了可灵活的配置,提高了系统的可靠性。

    原文作者:AOP
    原文地址: https://blog.csdn.net/l82153402/article/details/39083699
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞