一篇文章带你了解Spring AOP 的注解

 更新时间:2022年1月20日 13:15  点击:224 作者:YSOcean

1、xml 的方式实现 AOP

①、接口 UserService

package com.ys.aop;
public interface UserService {
    //添加 user
    public void addUser();
    //删除 user
    public void deleteUser();
}

②、实现类 UserServiceImpl

package com.ys.aop;
public class UserServiceImpl implements UserService{
    @Override
    public void addUser() {
        System.out.println("增加 User");
    }
    @Override
    public void deleteUser() {
        System.out.println("删除 User");
    }
}

③、切面类,也就是通知类 MyAspect

package com.ys.aop;
import org.aspectj.lang.JoinPoint;
public class MyAspect {
    /**
     * JoinPoint 能获取目标方法的一些基本信息
     * @param joinPoint
     */
    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知 : " + joinPoint.getSignature().getName());
    }
    public void myAfterReturning(JoinPoint joinPoint,Object ret){
        System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret);
    }
    public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("抛出异常通知 : " + e.getMessage());
    }
    public void myAfter(){
        System.out.println("最终通知");
    }
}

④、AOP配置文件 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: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/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">   
    <!--1、创建目标类 -->
    <bean id="userService" class="com.ys.aop.UserServiceImpl"></bean>  
    <!--2、创建切面类(通知)  -->
    <bean id="myAspect" class="com.ys.aop.MyAspect"></bean>
    <!--3、aop编程 
        3.1 导入命名空间
        3.2 使用 <aop:config>进行配置
                proxy-target-class="true" 声明时使用cglib代理
                如果不声明,Spring 会自动选择cglib代理还是JDK动态代理
            <aop:pointcut> 切入点 ,从目标对象获得具体方法
            <aop:advisor> 特殊的切面,只有一个通知 和 一个切入点
                advice-ref 通知引用
                pointcut-ref 切入点引用
        3.3 切入点表达式
            execution(* com.ys.aop.*.*(..))
            选择方法         返回值任意   包             类名任意   方法名任意   参数任意
    -->
    <aop:config>
        <aop:aspect ref="myAspect">
        <!-- 切入点表达式 -->
        <aop:pointcut expression="execution(* com.ys.aop.*.*(..))" id="myPointCut"/>
        <!-- 3.1 前置通知
                <aop:before method="" pointcut="" pointcut-ref=""/>
                    method : 通知,及方法名
                    pointcut :切入点表达式,此表达式只能当前通知使用。
                    pointcut-ref : 切入点引用,可以与其他通知共享切入点。
                通知方法格式:public void myBefore(JoinPoint joinPoint){
                    参数1:org.aspectj.lang.JoinPoint  用于描述连接点(目标方法),获得目标方法名等
        -->
        <aop:before method="myBefore" pointcut-ref="myPointCut"/>
        <!-- 3.2后置通知  ,目标方法后执行,获得返回值
                <aop:after-returning method="" pointcut-ref="" returning=""/>
                    returning 通知方法第二个参数的名称
                通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){
                    参数1:连接点描述
                    参数2:类型Object,参数名 returning="ret" 配置的
        -->
        <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />
        <!-- 3.3 最终通知 -->        
        <aop:after method="myAfter" pointcut-ref="myPointCut"/>  
        </aop:aspect>
    </aop:config>
</beans>

⑤、测试

@Test
public void testAop(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService useService = (UserService) context.getBean("userService");
    useService.addUser();
    useService.deleteUser();
}

⑥、控制台打印结果

上面的例子很简单,就是在 UserService 的 addUser()方法和 deleteUser()方法增加前置通知和后置通知,这在实际操作中很好理解。比如这是和数据库打交道的话,那么我们在addUser() 或者deleteUser() 时,必须要在前面开始事务,操作完毕后提交事务。下面我们就用注解的方式来配置。

2、注解实现 AOP

①、导入相应的 jar 包,以及在 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: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/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">   
</beans>

②、注解配置 bean

xml配置:

<!--1、创建目标类 -->
<bean id="userService" class="com.ys.aop.UserServiceImpl"></bean>  
<!--2、创建切面类(通知)  -->
<bean id="myAspect" class="com.ys.aop.MyAspect"></bean>

注解配置:

目标类:  

切面类:  

③、配置扫描注解识别

这个我们在前面也讲过,上面配置的注解,Spring 如何才能识别这些类上添加了注解呢?我们必须告诉他。

applicationContext.xml 文件中添加如下配置:

<!-- 配置扫描注解类
        base-package:表示含有注解类的包名。
        如果扫描多个包,则下面的代码书写多行,改变 base-package 里面的内容即可!
    -->
    <context:component-scan base-package="com.ys.aop"></context:component-scan>

④、注解配置 AOP

一、我们用xml配置过如下:  

这是告诉 Spring 哪个是切面类。下面我们用注解配置

我们在切面类上添加 @Aspect 注解,如下:  

 

二、如何让 Spring 认识我们所配置的 AOP 注解?

光有前面的类注解扫描是不够的,这里我们要额外配置 AOP 注解识别。

我们在 applicationContext.xml 文件中增加如下配置:

<!--2、确定 aop 注解生效  -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

三、注解配置前置通知

我们先看 xml 配置前置通知如下:

<!-- 切入点表达式 -->
        <aop:pointcut expression="execution(* com.ys.aop.*.*(..))" id="myPointCut"/>
        <!-- 3.1 前置通知
                <aop:before method="" pointcut="" pointcut-ref=""/>
                    method : 通知,及方法名
                    pointcut :切入点表达式,此表达式只能当前通知使用。
                    pointcut-ref : 切入点引用,可以与其他通知共享切入点。
                通知方法格式:public void myBefore(JoinPoint joinPoint){
                    参数1:org.aspectj.lang.JoinPoint  用于描述连接点(目标方法),获得目标方法名等
        -->
        <aop:before method="myBefore" pointcut-ref="myPointCut"/>

那么注解的方式如下:  

四、注解配置后置通知

xml 配置后置通知:

<!-- 3.2后置通知  ,目标方法后执行,获得返回值
                <aop:after-returning method="" pointcut-ref="" returning=""/>
                    returning 通知方法第二个参数的名称
                通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){
                    参数1:连接点描述
                    参数2:类型Object,参数名 returning="ret" 配置的
        -->
        <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />

注意看,后置通知有个 returning="ret" 配置,这是用来获得目标方法的返回值的。

注解配置如下:  

五、测试

@Test
    public void testAopAnnotation(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_Annotation.xml");
        UserService useService = (UserService) context.getBean("userService");
        useService.addUser();
        useService.deleteUser();
    }

六、控制台打印结果  

3、注解改进  

我们可以看前置通知和后置通知的注解配置:  

注意看红色框住的部分,很显然这里是重复的,而且如果我们有多个通知方法,那就得在每个方法名都写上该注解,而且如果包名够复杂,也很容易写错。那么怎么办呢?

解决办法就是声明公共切入点:

①、在 切面类 MyAspect.java 中新增一个切入点方法 myPointCut(),然后在这个方法上添加@Pointcut 注解

  

②、那么前置通知和后置通知,我们可以进行如下改写配置:  

4、总结

上面我们只进行了前置通知和后置通知的讲解,还有比如最终通知、环绕通知、抛出异常通知等,配置方式都差不多,这里就不进行一一讲解了。然后我们看一下这些通知的注解:

@Aspect 声明切面,修饰切面类,从而获得 通知。

通知

  • @Before 前置
  • @AfterReturning 后置
  • @Around 环绕
  • @AfterThrowing 抛出异常
  • @After 最终

切入点

  • @PointCut ,修饰方法 private void xxx(){} 之后通过“方法名”获得切入点引用

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更多内容!

原文出处:https://www.cnblogs.com/ysocean/p/7531083.html

[!--infotagslink--]

相关文章

  • Spring AOP 对象内部方法间的嵌套调用方式

    这篇文章主要介绍了Spring AOP 对象内部方法间的嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-29
  • Spring Cloud 中@FeignClient注解中的contextId属性详解

    这篇文章主要介绍了Spring Cloud 中@FeignClient注解中的contextId属性详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-25
  • Springboot如何实现Web系统License授权认证

    这篇文章主要介绍了Springboot如何实现Web系统License授权认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-28
  • Swagger中@ApiIgnore注解的使用详解

    这篇文章主要介绍了Swagger中@ApiIgnore注解的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-21
  • 校验非空的注解@NotNull如何取得自定义的message

    这篇文章主要介绍了校验非空的注解@NotNull如何取得自定义的message,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-29
  • 如何在Spring WebFlux的任何地方获取Request对象

    这篇文章主要介绍了如何在Spring WebFlux的任何地方获取Request对象,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下...2021-01-26
  • 详解SpringCloudGateway内存泄漏问题

    这篇文章主要介绍了详解SpringCloudGateway内存泄漏问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-16
  • Spring为什么不推荐使用@Autowired注解详析

    @Autowired 注解的主要功能就是完成自动注入,使用也非常简单,但这篇文章主要给大家介绍了关于Spring为什么不推荐使用@Autowired注解的相关资料,需要的朋友可以参考下...2021-11-03
  • Springboot如何使用mybatis实现拦截SQL分页

    这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19
  • Java中lombok的@Builder注解的解析与简单使用详解

    这篇文章主要介绍了Java中lombok的@Builder注解的解析与简单使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-06
  • 处理@PathVariable注解允许参数为空、允许不传参数的问题

    这篇文章主要介绍了处理@PathVariable注解允许参数为空、允许不传参数的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-23
  • SpringMVC文件上传原理及实现过程解析

    这篇文章主要介绍了SpringMVC文件上传原理及实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-15
  • Mybatis用注解写in查询的实现

    这篇文章主要介绍了Mybatis用注解写in查询的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-13
  • Spring Data JPA 关键字Exists的用法说明

    这篇文章主要介绍了Spring Data JPA 关键字Exists的用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-10
  • 解决@Transactional注解事务不回滚不起作用的问题

    这篇文章主要介绍了解决@Transactional注解事务不回滚不起作用的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-23
  • tomcat启动完成执行 某个方法 定时任务(Spring)操作

    这篇文章主要介绍了tomcat启动完成执行 某个方法 定时任务(Spring)操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-25
  • 使用Maven 搭建 Spring MVC 本地部署Tomcat的详细教程

    这篇文章主要介绍了使用Maven 搭建 Spring MVC 本地部署Tomcat,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-08-16
  • Java Spring Cloud 负载均衡详解

    这篇文章主要介绍了Spring Cloud负载均衡及远程调用实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2021-09-18
  • SpringMvc自动装箱及GET请求参数原理解析

    这篇文章主要介绍了SpringMvc自动装箱及GET请求参数原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-19
  • Springboot使用thymeleaf动态模板实现刷新

    这篇文章主要介绍了Springboot使用thymeleaf动态模板实现刷新,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-08-31