在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  網(wǎng)絡(luò)安全/ Spring怎么自定義局部變量的注解,并在aop中攔截?

Spring怎么自定義局部變量的注解,并在aop中攔截?

自定義一個(gè)局部變量的注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.LOCAL_VARIABLE)
public @interface MyName {
    String ip() default "127.0.0.2";
}

想對(duì)那些標(biāo)記了這個(gè)注解的變量,進(jìn)行一個(gè)aop,然后做一些處理(例如輸出參數(shù)所賦的值"zhangsan")

    public void test() {
        @MyName
        String name = "Zhangsan";
        System.out.println("test method");
    }

我的aop應(yīng)該怎么寫?

回答
編輯回答
清夢(mèng)

1.定義一個(gè)攔截器實(shí)現(xiàn)MethodInterceptor接口

public class TestInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        MyName annotation = method.getAnnotation(MyName .class);
        //TO-DO 處理自己的業(yè)務(wù)邏輯
    }

}

2.在spring的配置文件中配置基于注解的aop

<bean id="myNameInterceptor" class="core.advice.myNameInterceptor" />

 <aop:config>
        <aop:advisor
                pointcut="@annotation(core.annotation.MyName)"
                advice-ref="myNameInterceptor" />
    </aop:config>
2017年8月19日 15:27
編輯回答
尛曖昧

利用AOP+JoinPoint就可以實(shí)現(xiàn),例子懶得寫,百度一大堆

2017年12月3日 02:55
編輯回答
礙你眼

這個(gè)難度挺大的吧。先不說(shuō)AOP了。 獲取某個(gè)方法中的臨時(shí)變量, 這個(gè)能實(shí)現(xiàn)嗎?

2017年1月14日 12:50
編輯回答
避風(fēng)港

這應(yīng)該是不可能的。
https://stackoverflow.com/que...

2018年7月24日 15:15