码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot aop使用指南

时间:2020-06-16 15:39:07      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:ann   ota   指南   throw   before   aop   targe   oid   tar   

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 // 环绕增强

Spring Boot aop使用指南

标签:ann   ota   指南   throw   before   aop   targe   oid   tar   

原文地址:https://www.cnblogs.com/memo20/p/13140593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!