メソッドインターセプト (AOP)
YMP フレームワークの AOP は、CGLIB の MethodInterceptor によって実装されたインターセプトに基づいており、次のアノテーションによって構成されます:
@Before: クラスまたはメソッドのプレインターセプターを設定し、pre を宣言するために使用されます。クラスの -interceptor クラスのすべてのメソッドに適用されます。
@After: クラスまたはメソッドのポストインターセプターを設定するために使用されます。クラスで宣言されたポストインターセプターは、クラスのすべてのメソッドに適用されます。クラス;
@Clean: クラス上のすべてまたは指定されたインターセプターをクリーンアップするために使用され、クリーンアップされたインターセプターは実行されません。
@ContextParam: コンテキスト パラメーターを設定するために使用され、主にパラメーターをインターセプターに渡すために使用されます。構成;
@Ignored: メソッドの宣言はすべてのインターセプター構成を無視します。
説明: @Ignored アノテーション付きメソッド、非パブリック メソッド、オブジェクト クラス メソッド、およびオブジェクト クラスのオーバーロードされたメソッドの宣言は処理されません。インターセプター。
例 1:
// 创建自定义拦截器 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(); } } }
例 2:
@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(); } } }
注
: アノテーションの value 属性を使用すると、次の形式を通じてフレームワークのグローバル パラメーターから xxx の値を取得できます。 $xxx@ContextParam