• Spring Boot aop使用指南


    1. 使用示例

    1.在pom中添加依赖

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

    2.使用aop注解

    public class Test{
        public static void main(String[] args) {
            ApplicationContext ac = (ApplicationContext) new AnnotationConfigApplicationContext(Cfg.class);
            Target target = (Target) ac.getBean("target");
            target.print();
        }
    }
    @Configuration
    @ComponentScan("com.example.springboot")
    class Cfg{
    
    }
    @Component 
    class Target{
        public void print(){
            System.out.println("target print");
        }
    }
    
    @Aspect
    @Component // 必须纳入容器管理
    class MyAspect{
        @Before("execution(* com.example.springboot.Target.print())")
        public void before(){
            System.out.println("before print");
        }
    }
    
    

    2. 增强类型

    @Before 
    @AfterReturning
    @AfterThrowing
    @After // 其实是AfterFinally增强
    @Around // 环绕增强
    
  • 相关阅读:
    单例类
    日期类2
    日历类
    日期转换类
    抓取网页内容并截图
    关于计时器与多线程
    让页面上图片不变形
    Thread 调用方法的方式
    语音放大缩小
    阻止Enter键回发到服务端Asp.net
  • 原文地址:https://www.cnblogs.com/memo20/p/13140593.html
Copyright © 2020-2023  润新知