Spring5框架七:AOP

it2022-12-27  44

什么是AOP

AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系。例如日志功能。日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能毫无关系。对于其他类型的代码,如安全性、异常处理和透明的持续性也是如此。这种散布在各处的无关的代码被称为横切(cross-cutting)代码,在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

而AOP技术则恰恰相反,它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为“Aspect”,即方面。所谓“方面”,简单地说,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。AOP代表的是一个横向的关系,如果说“对象”是一个空心的圆柱体,其中封装的是对象的属性和行为;那么面向方面编程的方法,就仿佛一把利刃,将这些空心圆柱体剖开,以获得其内部的消息。而剖开的切面,也就是所谓的“方面”了。然后它又以巧夺天功的妙手将这些剖开的切面复原,不留痕迹。

AOP的基本概念

Aspect(切面):通常是一个类,里面可以定义切入点和通知JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用Advice(通知):AOP在特定的切入点上执行的增强处理,有before,after,afterReturning,afterThrowing,aroundPointcut(切入点):就是带有通知的连接点,在程序中主要体现为书写切入点表达式AOP代理:AOP框架创建的对象,代理就是目标对象的加强。Spring中的AOP代理可以使JDK动态代理,也可以是CGLIB代理,前者基于接口,后者基于子类

通知方法

前置通知:在我们执行目标方法之前运行(@Before)后置通知:在我们目标方法运行结束之后 ,不管有没有异常(@After)返回通知:在我们的目标方法正常返回值后运行(@AfterReturning)异常通知:在我们的目标方法出现异常后运行(@AfterThrowing)环绕通知:动态代理, 需要手动执行joinPoint.procced()(其实就是执行我们的目标方法执行之前相当于前置通知, 执行之后就相当于我们后置通知(@Around)

Spring AOP

Spring中的AOP代理还是离不开Spring的IOC容器,代理的生成,管理及其依赖关系都是由IOC容器负责,Spring默认使用JDK动态代理,在需要代理类而不是代理接口的时候,Spring会自动切换为使用CGLIB代理,不过现在的项目都是面向接口编程,所以JDK动态代理相对来说用的还是多一些。

利用Spring实现AOP

1、利用IDEA创建一个Maven项目 2、在pom.xml中导入依赖

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>

3、创建一个接口

public interface UserService { void add(); void delete(); void update(); void select(); }

4.创建一个实现类

public class UserServiceImpl implements UserService{ public void add() { System.out.println("增加一条数据。。。"); } public void delete() { System.out.println("删除一条数据。。。"); } public void update() { System.out.println("修改一条数据。。。"); } public void select() { System.out.println("查询一条数据。。。"); } }

方式一:使用Spring的接口

5、创建一个Log类实现MethodBeforeAdvice接口

import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class Log implements MethodBeforeAdvice { //method:要执行的目标对象的方法 //args:参数 //target:目标对象 public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了"); } }

6、创建一个AfterLog类实现AfterReturningAdvice接口

import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class AfterLog implements AfterReturningAdvice { //returnValue:返回值 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行了" + method.getName() + "方法,返回结果为:" + returnValue); } }

7、创建一个xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="userService" class="com.www.service.UserServiceImpl"></bean> <bean id="log" class="com.www.log.Log"></bean> <bean id="afterLog" class="com.www.log.AfterLog"></bean> <!--方式一:使用原生SpringAPI接口--> <!--配置AOP:需要导入AOP的约束--> <aop:config> <!--切入点:expression:表达式,execution:要执行的位置--> <aop:pointcut id="pointcut" expression="execution(* com.www.service.UserServiceImpl.*(..))"/> <!--执行环绕增加--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor> </aop:config> </beans>

8、创建测试类

import com.www.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //动态代理代理的是接口 UserService userService = (UserService) context.getBean("userService"); userService.add(); userService.update(); userService.select(); userService.delete(); } }

9、测试结果

方式二:自定义实现AOP

创建一个类

public class Diy { public void before(){ System.out.println("方法执行前。。。"); } public void after(){ System.out.println("方法执行后。。。"); } }

在xml中编写

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="userService" class="com.www.service.UserServiceImpl"></bean> <!--方式二:自定义实现--> <bean id="diy" class="com.www.diy.Diy"></bean> <aop:config> <!--自定义切面,ref表示要引用的类--> <aop:aspect ref="diy"> <!--切入点--> <aop:pointcut id="point" expression="execution(* com.www.service.UserServiceImpl.*(..))"/> <!--通知--> <aop:before method="before" pointcut-ref="point"></aop:before> <aop:after method="after" pointcut-ref="point"></aop:after> </aop:aspect> </aop:config> </beans>

结果:

方式三:使用注解实现AOP

创建一个类

import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect // 标注此类为一个切面 public class Annotation { @Before("execution(* com.www.service.UserServiceImpl.*(..))") public void before(){ System.out.println("方法前。。。。"); } @After("execution(* com.www.service.UserServiceImpl.*(..))") public void after(){ System.out.println("方法后。。。。"); } }

在xml中编写

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="userService" class="com.www.service.UserServiceImpl"></bean> <!--方式三:利用注解--> <bean id="annotation" class="com.www.diy.Annotation"></bean> <!--开启注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

结果:

最新回复(0)