User-Profile-Image
hankin
  • 5
  • Java
  • Kotlin
  • Spring
  • Web
  • SQL
  • MegaData
  • More
  • Experience
  • Enamiĝu al vi
  • 分类
    • Zuul
    • Zookeeper
    • XML
    • WebSocket
    • Web Notes
    • Web
    • Vue
    • Thymeleaf
    • SQL Server
    • SQL Notes
    • SQL
    • SpringSecurity
    • SpringMVC
    • SpringJPA
    • SpringCloud
    • SpringBoot
    • Spring Notes
    • Spring
    • Servlet
    • Ribbon
    • Redis
    • RabbitMQ
    • Python
    • PostgreSQL
    • OAuth2
    • NOSQL
    • Netty
    • MySQL
    • MyBatis
    • More
    • MinIO
    • MegaData
    • Maven
    • LoadBalancer
    • Kotlin Notes
    • Kotlin
    • Kafka
    • jQuery
    • JavaScript
    • Java Notes
    • Java
    • Hystrix
    • Git
    • Gateway
    • Freemarker
    • Feign
    • Eureka
    • ElasticSearch
    • Docker
    • Consul
    • Ajax
    • ActiveMQ
  • 页面
    • 归档
    • 摘要
    • 杂图
    • 问题随笔
  • 友链
    • Spring Cloud Alibaba
    • Spring Cloud Alibaba - 指南
    • Spring Cloud
    • Nacos
    • Docker
    • ElasticSearch
    • Kotlin中文版
    • Kotlin易百
    • KotlinWeb3
    • KotlinNhooo
    • 前端开源搜索
    • Ktorm ORM
    • Ktorm-KSP
    • Ebean ORM
    • Maven
    • 江南一点雨
    • 江南国际站
    • 设计模式
    • 熊猫大佬
    • java学习
    • kotlin函数查询
    • Istio 服务网格
    • istio
    • Ktor 异步 Web 框架
    • PostGis
    • kuangstudy
    • 源码地图
    • it教程吧
    • Arthas-JVM调优
    • Electron
    • bugstack虫洞栈
    • github大佬宝典
    • Sa-Token
    • 前端技术胖
    • bennyhuo-Kt大佬
    • Rickiyang博客
    • 李大辉大佬博客
    • KOIN
    • SQLDelight
    • Exposed-Kt-ORM
    • Javalin—Web 框架
    • http4k—HTTP包
    • 爱威尔大佬
    • 小土豆
    • 小胖哥安全框架
    • 负雪明烛刷题
    • Kotlin-FP-Arrow
    • Lua参考手册
    • 美团文章
    • Java 全栈知识体系
    • 尼恩架构师学习
    • 现代 JavaScript 教程
    • GO相关文档
    • Go学习导航
    • GoCN社区
    • GO极客兔兔-案例
    • 讯飞星火GPT
    • Hollis博客
    • PostgreSQL德哥
    • 优质博客推荐
    • 半兽人大佬
    • 系列教程
    • PostgreSQL文章
    • 云原生资料库
    • 并发博客大佬
Help?

Please contact us on our email for need any support

Support
    首页   ›   Spring   ›   SpringBoot   ›   正文
SpringBoot

SpringBoot—AOP的用法

2020-03-20 20:36:55
1040  0 0

阅读完需:约 2 分钟

在springboot里aop和spring里的用法是一样的只不过是没了xml配置改用注解来编写,aop的作用很大可以用来测试程序,增加新的功能等。

实践:

在IDEA里有时候无法自动加载AOP的依赖我们可以来手动加载它:

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

加载完依赖后创建 UserService 文件

@Service
public class UserService {
    public String getUsernameById(Integer id){
        System.out.println("id");
        return "hello";
    }

    public void deleteUsernameById(Integer id){
        System.out.println("id22222");
    }
}

我们可以先不用连接数据库,毕竟这个是用来测试的重点是AOP而不是数据库!

接着创建UserController文件

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/test1")
    public String getUsernameById(Integer id){
       return userService.getUsernameById(id);
    }

    @GetMapping("/test2")
    public void deleteUsernameById(Integer id){
        userService.deleteUsernameById(id);
    }

}

最后是重头戏 LogComponent 文件:

@Component
@Aspect
public class LogComponent {
    @Pointcut("execution(* cn.aop.*.*(..))") //在cn.aop包下的任意方法任意参数
    public void pcl(){
    }

    @Before(value="pcl()")
    public void before(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("前置通知"+name);
    }

    @After(value = "pcl()")
    public void after(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("后置通知"+name);
    }

    @AfterReturning(value = "pcl()",returning = "result")
    public void afterreturning(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回通知"+name+result);
    }

    @AfterThrowing(value = "pcl()",throwing = "e")
    public  void afterthrowing(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println("异常通知"+name+e.getMessage());
    }

    @Around(value = "pcl()") //环绕通知
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            //前置通知
            proceedingJoinPoint.proceed();
            //后置通知
        }catch (Exception e){
            //异常通知
        }finally {
            //最终通知
        }

        return "环绕通知";
    }

}

在游览器中输入测试的地址:http://localhost:8080/test1

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
Enamiĝu al vi
不要为明天忧虑.因为明天自有明天的忧虑.一天的难处一天当就够了。
543文章 68评论 294点赞 594023浏览

随机文章
SpringCloud—OpenFeign(二)参数传递
5年前
JavaPoet—代码生成框架
3年前
vue中路由传参
5年前
SpringMVC—HandlerAdapter—HttpMessageConverter消息转换器
3年前
Java —final 和 effectively final区别
5年前
博客统计
  • 日志总数:543 篇
  • 评论数目:68 条
  • 建站日期:2020-03-06
  • 运行天数:1927 天
  • 标签总数:23 个
  • 最后更新:2024-12-20
Copyright © 2025 网站备案号: 浙ICP备20017730号 身体没有灵魂是死的,信心没有行为也是死的。
主页
页面
  • 归档
  • 摘要
  • 杂图
  • 问题随笔
博主
Enamiĝu al vi
Enamiĝu al vi 管理员
To be, or not to be
543 文章 68 评论 594023 浏览
测试
测试
看板娘
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付