Spring AOP详解

1.Spring AOP

1.1 什么是AOP?

AOP(Aspect-Oriented Programming)面向切面编程,作为面向对象的一种补充,将公共逻辑(事务管理、日志、缓存等)封装成切面,跟业务代码进行分离,可以减少系统的重复代码和降低模块之间的耦合度,并有利于未来的可拓展性和可维护性。

1.2 AOP有哪些实现方式?

AOP有两种实现方式:静态代理和动态代理。

  • 静态代理

静态代理:代理类在编译阶段生成,在编译阶段将通知织入Java字节码中,也称编译时增强。AspectJ使用的是静态代理。

缺点:代理对象需要与目标对象实现一样的接口,并且实现接口的方法,会有冗余代码。同时,一旦接口增加方法,目标对象与代理对象都要维护。

  • 动态代理

动态代理:代理类在程序运行时创建,AOP框架不会去修改字节码,而是在内存中临时生成一个代理对象,在运行期间对业务方法进行增强,不会生成新类。

如果要代理的对象,实现了某个接口,那么 Spring AOP 会使用 JDK Proxy,去创建代理对象,而对于没有实现接口的对象,就无法使用 JDK Proxy 去进行代理了,这时候 Spring AOP 会使用 Cglib 生成一个被代理对象的子类来作为代理。
具体可参考:动态代理详解

1.3 AOP 切面编程涉及到的一些专业术语

术语含义
目标(Target)被通知的对象
代理(Proxy)向目标对象应用通知之后创建的代理对象
连接点(JoinPoint)目标对象的所属类中,定义的所有方法均为连接点
切入点(Pointcut)被切面拦截 / 增强的连接点(切入点一定是连接点,连接点不一定是切入点)
通知(Advice)增强的逻辑 / 代码,也即拦截到目标对象的连接点之后要做的事情
切面(Aspect)切入点(Pointcut)+通知(Advice)
Weaving(织入)将通知应用到目标对象,进而生成代理对象的过程动作

1.4 通知类型

  • Before(前置通知):目标对象的方法调用之前触发
  • After (后置通知):目标对象的方法调用之后触发
  • AfterReturning(返回通知):目标对象的方法调用完成,在返回结果值之后触发
  • AfterThrowing(异常通知):目标对象的方法运行中抛出 / 触发异常后触发。AfterReturning 和 - AfterThrowing 两者互斥。如果方法调用成功无异常,则会有返回值;如果方法抛出了异常,则不会有返回值。
  • Around (环绕通知):编程式控制目标对象的方法调用。环绕通知是所有通知类型中可操作范围最大的一种,因为它可以直接拿到目标对象,以及要执行的方法,所以环绕通知可以任意的在目标对象的方法调用前后搞事,甚至不调用目标对象的方法。

2.Spring的AOP配置

2.1 创建工程

创建工程

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.by</groupId>
    <artifactId>Spring_AOP_Xml</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Spring常用依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
         <!--支持切点表达式 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
    </dependencies>
</project>
  • dao层
/**
 * 持久层实现类
 */
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}
  • service层
/**
 * 业务层实现类
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;
    
    public void setUserDao(UserDao userDao){
        this.userDao=userDao;
    }
	
    @Override
    public void addUser(){
        userDao.addUser();
    }
}
  • applicationContext,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: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/aop
       			   http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <bean id="userDao" class="com.by.dao.UserDaoImpl"></bean>
    <bean id="userService" class="com.by.service.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>
  • web层
/**
 * 模拟表现层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用对象
        UserService userService = ac.getBean("userService",UserService.class);
        System.out.println(userService.getClass());
        userService.addUser();
    }
}

2.2 增强

  1. 创建增强类
   package com.by.advice;
   
   import org.aspectj.lang.ProceedingJoinPoint;
   
   import java.util.Date;
   
   public class MyLogAdvice {
   
       //前置通知
       public void before(){
           System.out.println("前置通知");
       }
   
       //后置通知【try】
       public void afterReturning(){
           System.out.println("后置通知");
       }
   
       //异常通知【catch】
       public void afterThrowing(){
           System.out.println("异常通知");
       }
   
       //最终通知【finally】
       public void after(){
           System.out.println("最终通知");
       }
   
       //环绕通知
       public void around(ProceedingJoinPoint joinPoint){
           try {
               System.out.println("方法执行前的环绕通知");
               joinPoint.proceed();
               System.out.println("方法执行后的环绕通知");
           } catch (Throwable throwable) {
               throwable.printStackTrace();
           }
       }
   }
  1. 配置增强类
   <!--增强-->
   <bean id="myLogger" class="com.by.advice.MyLogger"></bean>

2.3 切点

  1. 切点表达式

    • 表达式语法:

    execution([修饰符] 返回值类型 包名.类名.方法名(参数))

    • 例如:
    execution(* com.by.service.UserService.add(..))
    execution(* com.by.service.UserService.*(..))
    execution(* com.by.service.*.*(..))
    
    • 通配符解释:
      * :匹配任意字符,只匹配⼀个元素(返回类型, 包, 类名, 方法或者方法参数)
      .. :匹配多个连续的任意符号, 可以通配任意层级的包, 或任意类型, 任意个数的参数
  2. 配置切点

       <aop:config>
           <!--切点-->
           <aop:pointcut id="pointcut" expression="execution(* com.by.service.*.*(..))"/>
       </aop:config>
    

2.4 切面

  1. 增强的类型

    • aop:before:用于配置前置通知
    • aop:after-returning:用于配置后置【try】通知,它和异常通知只能有一个执行
    • aop:after-throwing:用于配置异常【catch】通知,它和后置通知只能执行一个
    • aop:after:用于配置最终【finally】通知
    • aop:around:用于配置环绕通知
  2. 各类增强的执行位置

    //before:前置通知
    //around:环绕通知
    try{
    	...
    	//after-returning:后置通知
    }catch(Execption e){
    	...
    	//after-throwing:异常通知
    }finally{
    	//finally:最终通知
    }
    //around:环绕通知
    
    
  3. 配置切面

       <!--切面-->
       <aop:aspect ref="myLogger">
           <!-- 用于配置前置通知:指定增强的方法在切入点方法之前执行 
       		method:用于指定通知类中的增强方法名称
       		ponitcut-ref:用于指定切入点
       	-->
       	<aop:before method="before" pointcut-ref="pointcut"/>
           <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
           <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>
           <aop:after method="after" pointcut-ref="pointcut"/>
           <aop:around method="around" pointcut-ref="pointcut"/>
       </aop:aspect>
    

2.5 测试

  1. 测试service实现接口时的类型(JDK动态代理)
    实现接口

  2. 测试service不实现接口时的类型(Cglib动态代理)

    不实现接口

3.基于注解的AOP配置

3.1.创建工程

创建工程

  • pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.by</groupId>
        <artifactId>Spring_AOP_Annotation</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!-- Spring常用依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>5.1.8.RELEASE</version>
            </dependency>
        </dependencies>
    </project>
    
  • dao

    @Repository
    public class UserDaoImpl implements UserDao {
    
        @Override
        public void addUser(){
            System.out.println("insert into tb_user......");
        }
    }
    
  • service

    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDao userDao;
    
        public void addUser() {
            userDao.addUser();
        }
    }
    
  • applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            		http://www.springframework.org/schema/beans/spring-beans.xsd
            		http://www.springframework.org/schema/aop
            		http://www.springframework.org/schema/aop/spring-aop.xsd
            		http://www.springframework.org/schema/context
            		http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.by"></context:component-scan>
    </beans>
    
  • 测试

    /**
     * 模拟表现层
     */
    public class Client {
        public static void main(String[] args) {
            ApplicationContext ac = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
            //使用对象
            UserService userService = ac.getBean("userServiceImpl",UserService.class);
            userService.addUser();
        }
    }
    

3.2.增强

  1. 常用注解

    • @Aspect:把当前类声明为切面类
    • @Before:前置通知,可以指定切入点表达式
    • @AfterReturning:后置【try】通知,可以指定切入点表达式
    • @AfterThrowing:异常【catch】通知,可以指定切入点表达式
    • @After:最终【finally】通知,可以指定切入点表达式
    • @Around:环绕通知,可以指定切入点表达式
  2. 开启spring对注解AOP的支持

    <!-- applicationContext.xml-->
    <!-- 开启spring对注解AOP的支持 -->
    <aop:aspectj-autoproxy/>
    
  3. 注解方式实现aop

    @Component
    @Aspect
    public class MyLogAdvice {
    
        //前置通知
        @Before("execution(* com.by.service.*.*(..))")
        public void before(){
            System.out.println("前置通知");
        }
    
    
        //后置通知【try】
        @AfterReturning("execution(* com.by.service.*.*(..))")
        public void afterReturning(){
            System.out.println("后置通知");
        }
    
        //异常通知【catch】
        @AfterThrowing("execution(* com.by.service.*.*(..))")
        public void afterThrowing(){
            System.out.println("异常通知");
        }
    
    
        //最终通知【finally】
        @After("execution(* com.by.service.*.*(..))")
        public void after(){
            System.out.println("最终通知");
        }
    
        //环绕通知
        @Around("execution(* com.by.service.*.*(..))")
        public void around(ProceedingJoinPoint joinPoint){
            try {
                System.out.println("方法执行前的环绕通知");
                joinPoint.proceed();
                System.out.println("方法执行后的环绕通知");
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
    
  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring AOP(面向切面编程)是Spring框架中的一个模块,用于提供横切关注点(Cross-Cutting Concerns)的支持。横切关注点是与应用程序的核心业务逻辑无关的功能,例如日志记录、性能统计、事务管理等。 在Spring AOP中,通过定义切面(Aspect)来捕获横切关注点,并将其应用到目标对象的方法中。切面由切点(Pointcut)和通知(Advice)组成。切点定义了在何处应用通知,通知则定义了在切点处执行的操作。 Spring AOP支持以下几种类型的通知: 1. 前置通知(Before Advice):在目标方法执行之前执行的通知。 2. 后置通知(After Advice):在目标方法执行之后执行的通知,不管方法是否抛出异常。 3. 返回通知(After Returning Advice):在目标方法成功执行并返回结果后执行的通知。 4. 异常通知(After Throwing Advice):在目标方法抛出异常后执行的通知。 5. 环绕通知(Around Advice):围绕目标方法执行的通知,可以在方法调用前后执行自定义操作。 除了通知,Spring AOP还支持引入(Introduction)和切点表达式(Pointcut Expression)等功能。引入允许为目标对象添加新的接口和实现,而切点表达式则允许开发人员定义切点的匹配规则。 要在Spring应用程序中使用AOP,需要进行以下步骤: 1. 引入Spring AOP的依赖。 2. 配置AOP代理。 3. 定义切面和通知。 4. 配置切点和通知之间的关系。 总之,Spring AOP提供了一种便捷的方式来处理横切关注点,使得开发人员可以将关注点与核心业务逻辑分离,提高代码的可维护性和可重用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这河里吗l

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值