@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Logged {
Event event();
System system();
}
我的观点是:
@Aspect
@Component
public class Aspect {
@Pointcut("@annotation(Logged) && args(event, system)")
public void invoke(Event event, System system) { }
@Around("invoke(event, system)")
public void aspectMethod (ProceedingJoinPoint, Event event, System system) {
System.out.println(event + " " + system);
}
事件和系统是枚举。
并在类似这样的方法之前添加了注释:
@Logged(event = Event.USER_LOGGED, system = System.WIN)
someTestingMethod();
只有当我将方面保留为:
@Aspect
@Component
public class Aspect {
@Pointcut("@annotation(Logged)")
public void invoke() { }
@Around("invoke()")
public void aspectMethod (ProceedingJoinPoint) {
System.out.println("Hey");
}