定義了一個注解,代碼如下
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface PreVisit {
String value();
}在Controller中使用這個注解,注意注解中的引數是一個方法@pv.hasAccess('xxxxxx'):
@PreVisit("@pv.hasAccess('xxxxxx')")
@RequestMapping(value = "getUser")
public User getUser(Integer userId) {...some code...}定義一個Service的方法,這個是注解的當引數的方法,代碼如下
@Service("pv")
public class PageVisit{
public boolean hasAccess(String par){
//some code
return false or true;
}
}現在的問題是:在AOP實作中,如何執行注解引數中的方法"pv.hasAccess('xxxxxx')"并得到結果
@Aspect
@Component
public class PreVisitAspect {
@Around("@annotation(preVisit)")
public Object around(ProceedingJoinPoint joinPoint,PreVisit preVisit) throws Throwable {
String value=https://bbs.csdn.net/topics/preVisit.value()//這里value的值是 @pv.hasAccess('xxxxxx')
//如何在這里得到這里執行 pv.hasAccess('xxxxxx')以后的結果,
//如boolean [email protected]('xxxxxx'); //結果是:false or true
//拿到結果后do something....
}
}
這里的@Around注解中的引數就可以是一個方法,但是不知道是如何實作的。
當然可以直接在AOP中用
@Autowired
PageVisit pv;
然后在around里面呼叫pv.hasAccess('xxx')
但這樣不夠靈活,因為這個方法有可能不同的地方執行的方法不一樣, 不然多個方法要寫多個AOP
@PreVisit("@serviceA.methodA('xxxxxx')")
@PreVisit("@serviceB.methodB('xxxxxx')")
@PreVisit("@serviceC.methodC('xxxxxx')")
........轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240261.html
標籤:Java SE
