方法攔截(AOP)

2018-12-24 21:51 更新

YMP框架的AOP是基于CGLIB的MethodInterceptor實(shí)現(xiàn)的攔截,通過以下注解進(jìn)行配置:

  • @Before:用于設(shè)置一個(gè)類或方法的前置攔截器,聲明在類上的前置攔截器將被應(yīng)用到該類所有方法上;

  • @After:用于設(shè)置一個(gè)類或方法的后置攔截器,聲明在類上的后置攔截器將被應(yīng)用到該類所有方法上;

  • @Clean:用于清理類上全部或指定的攔截器,被清理的攔截器將不會被執(zhí)行;

  • @ContextParam:用于設(shè)置上下文參數(shù),主要用于向攔截器傳遞參數(shù)配置;

  • @Ignored:聲明一個(gè)方法將忽略一切攔截器配置;

說明:

聲明@Ignored注解的方法、非公有方法和Object類方法及Object類重載方法將不被攔截器處理。

示例一:

    // 創(chuàng)建自定義攔截器
    public class DemoInterceptor implements IInterceptor {
        public Object intercept(InterceptContext context) throws Exception {
            // 判斷當(dāng)前攔截器執(zhí)行方向
            switch (context.getDirection()) {
                // 前置
                case BEFORE:
                    System.out.println("before intercept...");
                    // 獲取攔截器參數(shù)
                    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 "攔截器參數(shù)傳遞";
        }

        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();
            }
        }
    }

示例二:

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

        public String beforeTest() {
            return "默認(rèn)前置攔截測試";
        }

        @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();
            }
        }
    }

@ContextParam注解的value屬性允許通過$xxx的格式支持從框架全局參數(shù)中獲取xxx的值

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號