SpringBoot——面向切面编程AOP

r囧r小猫 2022-05-24 01:15 368阅读 0赞

SpringBoot中使用AOP,和以前Spring中使用AOP没什么区别。如果你连AOP是什么都不清楚,请先看这篇博客:https://blog.csdn.net/u010837612/article/details/45583581

在SpringBoot中使用AOP,导入的依赖包不太一样:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-aop</artifactId>
  4. <version>2.0.2.RELEASE</version>
  5. </dependency>

具体使用方法都和Spring中一样的,具体可以看上面给出的链接。这里也给出一个简单的例子。

先写一个Controller,输出hello

  1. package com.example.demo.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @GetMapping("/hello")
  7. public String hello(){
  8. System.out.println("hello");
  9. return "";
  10. }
  11. }

然后写一个切面类:

  1. package com.example.demo.ascept;
  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 LogAspect {
  9. @Before("execution(* com.example.demo.controller.*.*(..))")
  10. public void before(){
  11. System.out.println("before......");
  12. }
  13. @After("execution(* com.example.demo.controller.*.*(..))")
  14. public void after(){
  15. System.out.println("after......");
  16. }
  17. }

运行,然后访问localhost:8080/hello,查看控制台
这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读

    相关 面向切面编程AOP

    1. 定义 AOP意为:[面向切面编程][Link 1],通过[预编译][Link 2]方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP可以对业务逻辑的各...

    相关 AOP面向切面编程

    [1.引言][1.]         原文地址:[点击打开链接][1.] 软件开发的目标是要对世界的部分元素或者信息流建立模型,实现软件系统的工程需要将系统分解成可以创

    相关 AOP面向切面编程

    AOP 面向切面编程 1.使用场景还原 当我们打开京东 app 进入首页,如果当前是没有网络的状态,里面的按钮点击是没有反应的。只有当我们打开网络的情况下,点击按钮