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接口

  • 相关阅读:
    关于vue2.x使用axios以及http-proxy-middleware代理处理跨域的问题
    vue-resource的使用
    从头开始开发一个vue幻灯片组件
    图与例解读Async/Await
    浅谈web缓存
    APICloud框架——总结一下最近开发APP遇到的一些问题 (三)
    编写现代 CSS 代码的 20 个建议
    仿微信联系人列表滑动字母索引
    初来乍到,向各位大牛虚心学习
    转发80端口的脚本
  • 原文地址:https://www.cnblogs.com/vwater/p/11459257.html
Copyright © 2011-2022 走看看