Method interception (AOP)


The AOP of the YMP framework is based on the interception implemented by CGLIB's MethodInterceptor. It is configured through the following annotations:

  • @Before: used to set the prefix of a class or method Interceptor, the pre-interceptor declared on the class will be applied to all methods of the class;

  • @After: used to set the post-interceptor of a class or method, Post-interceptors declared on a class will be applied to all methods of the class;

  • @Clean: used to clean all or specified interceptors on the class, the cleaned interceptors The interceptor will not be executed;

  • @ContextParam: used to set context parameters, mainly used to pass parameter configuration to the interceptor;

  • @Ignored: Declaring a method will ignore all interceptor configurations;

Description:

Declare @Ignored annotated methods, non-public methods and Object class methods And the overloaded methods of the Object class will not be processed by the interceptor.

Example one:

    // 创建自定义拦截器
    public class DemoInterceptor implements IInterceptor {
        public Object intercept(InterceptContext context) throws Exception {
            // 判断当前拦截器执行方向
            switch (context.getDirection()) {
                // 前置
                case BEFORE:
                    System.out.println("before intercept...");
                    // 获取拦截器参数
                    String _param = context.getContextParams().get("param");
                    if (StringUtils.isNotBlank(_param)) {
                        System.out.println(_param);
                    }
                    break;
                // 后置
                case AFTER:
                    System.out.println("after intercept...");
            }
            return null;
        }
    }

    @Bean
    public class TestDemo {

        @Before(DemoInterceptor.class)
        public String beforeTest() {
            return "前置拦截测试";
        }

        @After(DemoInterceptor.class)
        public String afterTest() {
            return "后置拦截测试";
        }

        @Before(DemoInterceptor.class)
        @After(DemoInterceptor.class)
        @ContextParam({
                @ParamItem(key = "param", value = "helloworld")
            })
        public String allTest() {
            return "拦截器参数传递";
        }

        public static void main(String[] args) throws Exception {
            YMP.get().init();
            try {
                TestDemo _demo = YMP.get().getBean(TestDemo.class);
                _demo.beforeTest();
                _demo.afterTest();
                _demo.allTest();
            } finally {
                YMP.get().destroy();
            }
        }
    }

Example two:

    @Bean
    @Before(DemoInterceptor.class)
    @ContextParam({
            @ParamItem(key = "param", value = "helloworld")
        })
    public class TestDemo {

        public String beforeTest() {
            return "默认前置拦截测试";
        }

        @After(DemoInterceptor.class)
        public String afterTest() {
            return "后置拦截测试";
        }

        @Clean
        public String cleanTest() {
            return "清理拦截器测试";
        }

        public static void main(String[] args) throws Exception {
            YMP.get().init();
            try {
                TestDemo _demo = YMP.get().getBean(TestDemo.class);
                _demo.beforeTest();
                _demo.afterTest();
                _demo.cleanTest();
            } finally {
                YMP.get().destroy();
            }
        }
    }

Note:@ContextParamThe value attribute of the annotation allows the format of $xxx to be used to obtain the value of xxx from the framework global parameters