Spring(十四):SpringAOP及AOP的三种实现方法

[复制链接]
发表于 2022-9-29 12:48:54 | 显示全部楼层 |阅读模式

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

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

×
一、什么是AOP
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
二、AOP的一些概念
1.Aspect(切面):切面是通知和切入点的结合。
2.Join point(连接点):与切入点匹配的执行点,例如执行方法或处理异常。在SpringAOP 中,连接点始终表示方法执行。
3.Advice(通知):在切面中需要完成的工作。
4.Pointcut(切入点):切入通知执行的位置。
5.Target(目标):被通知的对象。
6. proxy(代理):向目标对象应用通知之后创建的对象。
7.Introduction(引入):向现有类添加新的方法或属性。
8.Weaving(织入):将各个方面与其他应用程序类型或对象链接起来,以创建通知的对象。
三、AOP的三种实现方法
首先我们需要导入依赖:
  1.      <dependency>
  2.             <groupId>org.aspectj</groupId>
  3.             <artifactId>aspectjweaver</artifactId>
  4.             <version>1.9.9.1</version>
  5.         </dependency>
复制代码
1.通过SpringAPI接口进行实现
SpringAOP有五种通知方式,也有对应的接口:
Before:前置通知,在目标方法调用前通知,对应接口:MethodBeforeAdvice;
After:后置通知,在目标方法返回或异常后通知,对应接口:AfterReturningAdvice;
AfterReturning:后置返回通知,在目标方法返回后通知,对应接口:AfterReturningAdvice;
AfterThrowing:异常通知,在目标方法抛出异常后通知,对应接口:ThrowsAdvice;
Around:环绕通知:通知方法会将目标方法包裹起来,对应接口:MethodInterceptor;
我们下面以具体的例子来展示:
(1)定义一个接口
  1. package com.jms.service;
  2. public interface UserService {
  3.     void create();
  4.     void read();
  5.     void update();
  6.     void delete();
  7. }
复制代码
(2)接口的实现类
  1. package com.jms.service;
  2. public class UserServiceImpl implements UserService{
  3.     @Override
  4.     public void create() {
  5.         System.out.println("建立了一个用户信息");
  6.     }
  7.     @Override
  8.     public void read() {
  9.         System.out.println("读取了一个用户信息");
  10.     }
  11.     @Override
  12.     public void update() {
  13.         System.out.println("更新了一个用户信息");
  14.     }
  15.     @Override
  16.     public void delete() {
  17.         System.out.println("删除了了一个用户信息");
  18.     }
  19. }
复制代码
(3)建立两个类分别继承前置通知和后置返回通知的接口
  1. package com.jms.log;
  2. import org.springframework.aop.MethodBeforeAdvice;
  3. import java.lang.reflect.Method;
  4. public class beforeLog implements MethodBeforeAdvice {
  5.     @Override
  6.     //method:要执行目标对象的方法
  7.     //args:参数
  8.     //target:目标对象
  9.     public void before(Method method, Object[] args, Object target) throws Throwable {
  10.         System.out.println("[Debug]" + target.getClass().getName() + "的" + method.getName() + "执行...");
  11.     }
  12. }
复制代码
  1. package com.jms.log;
  2. import org.springframework.aop.AfterReturningAdvice;
  3. import java.lang.reflect.Method;
  4. public class afterLog implements AfterReturningAdvice{
  5.     //returnValue:返回值
  6.     @Override
  7.     public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
  8.         System.out.println("[Debug]" + target.getClass().getName() + "的" + method.getName() + "执行完成,返回了" + returnValue);
  9.     }
  10. }
复制代码
(4)xml配置文件
注册bean,并且配置aop
  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.     <bean id="userService" class="com.jms.service.UserServiceImpl"/>
  10.     <bean id="beforeLog" class="com.jms.log.beforeLog"/>
  11.     <bean id="afterLog" class="com.jms.log.afterLog"/>
  12.    
  13.    
  14.     <aop:config>
  15.         
  16.         <aop:pointcut id="pointcut" expression="execution(* com.jms.service.UserServiceImpl.*(..))"/>
  17.         
  18.         <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
  19.         <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  20.     </aop:config>
  21. </beans>
复制代码
(5)测试
  1. @Test
  2.     public void test1() {
  3.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  4.         /*
  5.         UserServiceImpl userService = applicationContext.getBean("userService", UserServiceImpl.class)这样执行会报错
  6.         因为动态代理代理的是接口,所以必须获取接口
  7.          */
  8.         UserService userService = applicationContext.getBean("userService", UserService.class);
  9.         userService.create();
  10.     }
复制代码
测试结果如下:

 
 
 2.自定义类、自定义切面实现
接口以及实现类都与上面相同
(1)自定义切面类
  1. package com.jms.diy;
  2. public class diyAspect {
  3.     public void before() {
  4.         System.out.println("[Debug]方法执行...");
  5.     }
  6.     public void after() {
  7.         System.out.println("[Debug]方法执行完成");
  8.     }
  9. }
复制代码
切面类中自定义通知方法
(2)xml配置文件
注入切面类的Bean,配置AOP
  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.     <bean id="userService" class="com.jms.service.UserServiceImpl"/>
  10.     <bean id="beforeLog" class="com.jms.log.beforeLog"/>
  11.     <bean id="afterLog" class="com.jms.log.afterLog"/>
  12.    
  13.    
  14.     <aop:config>
  15.         
  16.         <aop:pointcut id="pointcut" expression="execution(* com.jms.service.UserServiceImpl.*(..))"/>
  17.         
  18.         <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
  19.         <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  20.     </aop:config>
  21. </beans>                        
复制代码
(3)测试如下

 
 3.通过注解实现
这种实现其实是第二种的注解方式
(1)自定义切面类
  1. package com.jms.diy;
  2. import org.aspectj.lang.annotation.After;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.springframework.stereotype.Component;
  6. @Aspect
  7. @Component
  8. public class annotationAspect {
  9.     @Before("execution(* com.jms.service.UserServiceImpl.*(..))")
  10.     public void before() {
  11.         System.out.println("[Debug]方法前置增强");
  12.     }
  13.     @After("execution(* com.jms.service.UserServiceImpl.*(..))")
  14.     public void after() {
  15.         System.out.println("[Debug]方法后置增强");
  16.     }
  17. }
复制代码
(2)此处还是采用xml配置,也可以采用java类配置
  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.        xmlns:context="http://www.springframework.org/schema/context"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.         https://www.springframework.org/schema/beans/spring-beans.xsd
  8.         http://www.springframework.org/schema/aop
  9.         https://www.springframework.org/schema/aop/spring-aop.xsd
  10.         http://www.springframework.org/schema/context
  11.         https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <bean id="userService" class="com.jms.service.UserServiceImpl"/>
  13.    
  14.    
  15.     <context:annotation-config/>
  16.     <context:component-scan base-package="com.jms"/>
  17.     <aop:aspectj-autoproxy/>
  18.    
  19. </beans>
复制代码
(3)测试

 
(本文仅作个人学习记录用,如有纰漏敬请指正)
回复

使用道具 举报

登录后关闭弹窗

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