【发布时间】:2018-08-22 14:32:07
【问题描述】:
我有 2 个自定义注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface FlowPoint {
public enum PointInFlow {
START, END
}
PointInFlow pointInFlow();
}
和:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ScopeAnnotation {
public enum Category {
BUSINESS, DETECTION, INTERNAL_FUNC, THRESHOLD
}
Category category() default Category.DETECTION;
}
在我的代码中,我用PointInFlow.START 注释了一个方法,用Category.DETECTION 和Category.BUSINESS 注释了其他一些方法
我的切入点是:
@Pointcut("execution(* *(..)) && @annotation(flowPoint) && if()")
public static boolean executeStartMethod(<annotationPackage>.FlowPoint flowPoint) {
return flowPoint.pointInFlow() == FlowPoint.PointInFlow.START;}
@Before("executeStartMethod(flowPoint)")
public void beforeStartMethod(<annotationPackage>.FlowPoint flowPoint, JoinPoint jp) {
logger.infoBefore(jp, flowPoint.pointInFlow());}
@After("executeStartMethod(flowPoint)")
public void afterStartMethod(<annotationPackage>.annotation.FlowPoint flowPoint, JoinPoint jp) {
logger.infoAfter(jp);}
@Pointcut("execution(* *(..)) && @annotation(scopeAnnotation) && if()")
public static boolean executeDetectionMethod(<annotationPackage>.ScopeAnnotation scopeAnnotation) {
return scopeAnnotation.category() == ScopeAnnotation.Category.DETECTION;}
@Before("executeDetectionMethod(scopeAnnotation)")
public void beforeDetectionMethod(<annotationPackage>.ScopeAnnotation scopeAnnotation, JoinPoint jp) {
logger.infoBefore(jp, scopeAnnotation.category());}
@After("executeDetectionMethod(scopeAnnotation)")
public void afterDetectionMethod(<annotationPackage>.ScopeAnnotation scopeAnnotation, JoinPoint jp) {
logger.infoAfter(jp);}
@Pointcut("execution(* *(..)) && @annotation(scopeAnnotation) && if()")
public static boolean executeBusinessMethod(<annotationPackage>.ScopeAnnotation scopeAnnotation) {
return scopeAnnotation.category() == ScopeAnnotation.Category.BUSINESS;}
@Before("executeBusinessMethod(scopeAnnotation)")
public void beforeBusinessMethod(<annotationPackage>.ScopeAnnotation scopeAnnotation, JoinPoint jp) {
logger.infoBefore(jp, scopeAnnotation.category());}
@After("executeBusinessMethod(scopeAnnotation)")
public void afterBusinessMethod(<annotationPackage>.annotation.ScopeAnnotation scopeAnnotation, JoinPoint jp) {
logger.infoAfter(jp);}
问题是 DETECTION 和 BUSINESS 分别工作,(当我注释掉其中一个检测或业务切入点定义时。)但不是像上面那样一起工作。
提前感谢您的帮助
【问题讨论】:
标签: annotations aop aspectj aspect pointcut