zoukankan      html  css  js  c++  java
  • Spring之AOP

    1. 自定义一个注解

    import java.lang.annotation.*;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface AdminOnly {
    }

    2. 被代理类

    @Service
    public class TrainService {
        @AdminOnly
        public void move(){
            System.out.println("train move");
        }
    }

    3. 定义一个切面,包含pointcut和advise

    @Aspect
    @Component
    public class SecurityAspect {
        @Pointcut ("@annotation(AdminOnly)")
        public void adminOnly(){
    
        }
    
        @Before("adminOnly()")
        public void check(){
            System.out.println("测试自定义注解、aop");
        }
    }

    4. Test

    import com.djtu.vwater.VwaterApplication;
    import com.djtu.vwater.cglib.TrainService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = VwaterApplication.class)
    public class VwaterApplicationTest {
        @Autowired
        TrainService trainService;
        @Test
        public void trainMoveTest(){
            trainService.move();
        }
    }

    //测试结果

    测试自定义注解、aop
    train move

     

     在web开发中,都是分层开发,如controller、service、dao层,而这写被注解标注的类的生命周期都是托管给spring容器的,比如controller调用service层的服务,其实不是直接调用service实现类的接口,而是调用spring生成该service的代理类(如果是cglib代理,也就是service的子类;如果是jdk代理,则spring会动态生成一个代理),该代理类调用service时,spring会做一些额外的操作,这也就解释了,在debug模式下,在controller层调用service时,总会进入CglibAopProxy内部类DynamicAdvisedInterceptor的intercept函数中,DynamicAdvisedInterceptor实现了MethodInterceptor接口

  • 相关阅读:
    前端各种小细节
    图片等比例缩放
    微信小程序倒计时,购物车,向左滑删除 左拉删除
    微信小程序用户拒绝授权,重新调起授权
    微信小程序swiper切换卡内嵌滚动条不显示
    php面试总结
    tp5.0 学习(三):URL和路由
    pytorch 基础内容
    Grammarly for Chrome-语法、用词自动检查
    时空图神经网路:STGNNs
  • 原文地址:https://www.cnblogs.com/vwater/p/11459257.html
Copyright © 2011-2022 走看看