基于注解的切面开发方式

 springboot基于注解的切面开发方式,不用within和execution表达式方式
  • 首先自定义一个注解

@Retention(RetentionPolicy.RUNTIME) //运行时生效
@Target(ElementType.METHOD) //作用在方法上
public @interface Myadvice {
}

 

  • 其次编写切面方法

@Configuration
@Aspect
public class MyAspectConfig {
    //表示根据这个类的注解,进行切入 com.wbb.annotations.Myadvice 是一个注解的全限定名
    @After("@annotation(com.wbb.annotations.Myadvice)")
    public void after(JoinPoint joinPoint){
        System.out.println("==========后置通知操作======================");
    }
}

 

  • 在方法上使用,在哪个方法上使用,当调用这个方法时,就会进入切面

@Service
@Transactional
public class UserServiceImpl implements UserService {
​
    private UserDao userDao;
    
    @Myadvice
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}

 


 

posted @ 2021-07-20 16:42  neoQVQ  阅读(143)  评论(0)    收藏  举报