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

标题: 什么是AOP面向切面编程?怎么简单理解? [打印本页]

作者: 伤心客    时间: 2024-11-5 18:34
标题: 什么是AOP面向切面编程?怎么简单理解?
本文原文地点:什么是AOP面向切面编程?怎么简单理解?
什么是AOP面向切面编程

面向切面编程(AOP)通过将横切关注点(cross-cutting concerns)分离出来,提供了一种增强代码模块化和可维护性的方法。
简单来说,AOP就是将公共的模块封装成公共的方法,然后在需要的时间(这个就是切入点),直接就可以调用,而不用在各个对象里面具体的实现。
AOP是一种新的编程方式,它和OOP不同,OOP把体系看作多个对象的交互,AOP把体系分解为不同的关注点,或者称之为切面(Aspect)。这个可以理解为把体系理解为一个流程,一个对象负责流程上的一个节点。
当然,AOP和公共模块抽取调用的方式的差别在于切入点的调用方式的不同。AOP是通过某种方式(下面AOP原理会表明)自动的调用,而不管是抽取公共方法,还是通过Proxy模式实现调用,都需要在每个业务方法上重复编写调用。
以AOP的视角来编写上述业务,可以依次实现:
然后,以某种方式,让框架来把上述3个Aspect以Proxy的方式“织入”到BookService中,这样一来,就不必编写复杂而冗长的Proxy模式或者公共方法调用。
AOP原理

如何把切面织入到核心逻辑中?这正是AOP需要解决的问题。换句话说,假如客户端获得了BookService的引用,当调用bookService.createBook()时,如何对调用方法举行拦截,并在拦截前后举行安全检查、日志、事件等处理,就相当于完成了全部业务功能。
在Java平台上,对于AOP的织入,有3种方式:
最简单的方式是第三种,Spring的AOP实现就是基于JVM的动态代理。由于JVM的动态代理要求必须实现接口,假如一个普通类没有业务接口,就需要通过CGLIB或者Javassist这些第三方库实现。
AOP技能看上去比较神秘,但实际上,它本质就是一个动态代理,让我们把一些常勤奋能如权限检查、日志、事件等,从每个业务方法中剥离出来。
需要特别指出的是,AOP对于解决特定问题,例如事件管理非常有用,这是因为分散在各处的事件代码几乎是完全相同的,并且它们需要的参数(JDBC的Connection)也是固定的。另一些特定问题,如日志,就不那么容易实现,因为日志虽然简单,但打印日志的时间,经常需要捕获局部变量,假如利用AOP实现日志,我们只能输出固定格式的日志,因此,利用AOP时,必须恰当特定的场景。
核心概念

示例

以下是一个利用 Spring AOP 的简单示例,展示了如何定义和应用切面。
  1. import org.aspectj.lang.annotation.Aspect;
  2. import org.aspectj.lang.annotation.Before;
  3. @Aspect
  4. public class LoggingAspect {
  5.     @Before("execution(* com.example.service.*.*(..))")
  6.     public void logBeforeMethod() {
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.        xmlns:aop="http://www.springframework.org/schema/aop"
  9.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  11.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  12.                            http://www.springframework.org/schema/aop
  13.                            http://www.springframework.org/schema/aop/spring-aop.xsd">
  14.     <aop:aspectj-autoproxy/>
  15.     <bean id="loggingAspect" />
  16. </beans>System.out.println("Method is about to be executed");
  17.     }
  18. }
复制代码
在这个示例中,LoggingAspect 是一个切面,它包罗一个前置关照 logBeforeMethod。这个关照将在 com.example.service 包中的全部方法执行之前运行。
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:aop="http://www.springframework.org/schema/aop"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  6.                            http://www.springframework.org/schema/aop
  7.                            http://www.springframework.org/schema/aop/spring-aop.xsd">
  8.     <aop:aspectj-autoproxy/>
  9.     <bean id="loggingAspect" />
  10. </beans>
复制代码
  1. package com.example.service;public class UserService {    public void createUser() {<beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:aop="http://www.springframework.org/schema/aop"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  6.                            http://www.springframework.org/schema/aop
  7.                            http://www.springframework.org/schema/aop/spring-aop.xsd">
  8.     <aop:aspectj-autoproxy/>
  9.     <bean id="loggingAspect" />
  10. </beans>System.out.println("Creating user");    }}
复制代码
  1. import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.example.service.UserService;public class Main {    public static void main(String[] args) {<beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:aop="http://www.springframework.org/schema/aop"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  6.                            http://www.springframework.org/schema/aop
  7.                            http://www.springframework.org/schema/aop/spring-aop.xsd">
  8.     <aop:aspectj-autoproxy/>
  9.     <bean id="loggingAspect" />
  10. </beans>ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");<beans xmlns="http://www.springframework.org/schema/beans"
  11.        xmlns:aop="http://www.springframework.org/schema/aop"
  12.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  14.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  15.                            http://www.springframework.org/schema/aop
  16.                            http://www.springframework.org/schema/aop/spring-aop.xsd">
  17.     <aop:aspectj-autoproxy/>
  18.     <bean id="loggingAspect" />
  19. </beans>UserService userService = (UserService) context.getBean("userService");<beans xmlns="http://www.springframework.org/schema/beans"
  20.        xmlns:aop="http://www.springframework.org/schema/aop"
  21.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  23.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  24.                            http://www.springframework.org/schema/aop
  25.                            http://www.springframework.org/schema/aop/spring-aop.xsd">
  26.     <aop:aspectj-autoproxy/>
  27.     <bean id="loggingAspect" />
  28. </beans>userService.createUser();    }}
复制代码
运行这个示例时,输出将会是:
  1. Method is about to be executed
  2. Creating user
复制代码
这表明前置关照在 createUser 方法执行之前被调用了。
AOP 通过将横切关注点分离出来,提供了一种增强代码模块化和可维护性的方法。通过定义切面、连接点、切入点和关照,可以在不修改现有代码的情况下,动态地将横切关注点织入到程序中。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




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