广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot如何使用Aspectj实现AOP面向切面编程
  • 146
分享到

Springboot如何使用Aspectj实现AOP面向切面编程

2024-04-02 19:04:59 146人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录要在 SpringBoot中声明 AspectJ 切面引入jar包       网上也有说要在application.properties

要在 springboot中声明 AspectJ 切面

需在 ioc 容器中将切面声明为 Bean 实例 即加入@Component 注解;当在 Spring IOC 容器中初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.

在 AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java 类.

引入jar包       

网上都是说springboot使用Aspectj做面向切面编程的时候,只需要引入下面jar包依赖即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

但是我去编写的时候,单单引入 spring-boot-starter-aop 的jar依赖的时候,像@Component、@Aspect等這些註解都不能使用,後來發現缺少aspectjweaver 这么个jar包,最后引入了下面的jar才解決問題 

    <dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.5.3</version>
    </dependency> 

网上也有说要在application.properties中添加

spring.aop.auto=true这个配置,才能开启Aspectj注解的扫面,但是我去查询了springboot全局配置文件,里面默认配置为true(spring.aop.auto=true # Add @EnableAspectJAutoProxy),所以我没有去做添加,功能没有问题,切面能正常实现。

最后补充一点小知识

 AspectJ 支持 5 种类型的通知注解

1)@Before:  前置通知:在方法执行之前执行的通知

2)@After: 后置通知, 在方法执行之后执行 , 即方法返回结果或者抛出异常的时候, 下面的后置通知记录了方法的终止.

3)@AfterRunning: 返回通知, 在方法返回结果之后执行

ps:无论方法是正常返回还是抛出异常, 后置通知都会执行. 如果只想在方法返回的时候记录日志, 应使用返回通知代替后置通知.

4)@AfterThrowing: 异常通知, 在方法抛出异常之后

5) @Around: 环绕通知, 围绕着方法执行(即方法前后都有执行)

环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.

下面是我写的一些通知的实例

大家可以参考一下

        
 
    //@Before:  前置通知
    @Before("execution (* com.lc.project..controller..*.*(..))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().toString();
        Object result= Arrays.asList(joinPoint.getArgs());
            System.out.println("The method name:"+methodName+"--value:"+result);
    }
 
    //@After: 后置通知
    @After("execution (* *.*(..))")
    public void afterMethod(JoinPoint joinPoint){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method name:"+methodName+ " ends");
    }
    //@AfterRunning: 返回通知
    @AfterReturning(value="execution (* *.*(..))",returning="result")
    public void afterReturningMethod(JoinPoint joinPoint,Object result){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method name:"+methodName+ " ends and result="+result);
    }
    //@AfterThrowing: 异常通知
    @AfterThrowing(value="execution (* *.*(..))",throwing="e")
    public void afterReturningMethod(JoinPoint joinPoint,Exception e){
                String methodName = joinPoint.getSignature().getName();
                System.out.println("The method name:"+methodName+ " ends and result="+e);
    }  

  以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。               

--结束END--

本文标题: Springboot如何使用Aspectj实现AOP面向切面编程

本文链接: https://www.lsjlt.com/news/161472.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作