我可以从AspectJ中带注释 @Before 的方法返回。
@Before
public void simpleAdvice(JoinPoin joinPoint) {
if (smth == null)
/* return for method, which annotated */
}
如果我的问题不完整,请详细询问我。
答案 0 :(得分:0)
@Aspect
@SuppressAjWarnings({ "adviceDidNotMatch" })
public class TestAspect {
@Around("execution(@Monitor void *..*.* ())")
public void aroundMethodWithMonitorAnnotation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.err.println("Around - before a()");
String sessionId = (String) proceedingJoinPoint.getArgs()[0];
if(sessionId != null){
proceedingJoinPoint.proceed();
System.err.println("Around - after a()");
} else {
System.err.println("Around - a() not called");
}
}
public static void main(String[] args) {
new TestAspect().a();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Monitor {
}
@Monitor
public void a() {
System.err.println("a()");
}
}
答案 1 :(得分:0)
您可以使用@Before
,@After
,@AfterReturning
,@AfterThrowing
,@Around
来定义方法。但是您的课程可以在@Aspect
注册。
您还需要定义pointcut
和joinpoints
。
对于示例,
@Before(value="execution(* com.pointel.aop.AopTest.beforeAspect(..))")
public void beforeAdvicing(JoinPoint joinPoint){
String name = joinPoint.getSignature().getName();
System.out.println("Name of the method : "+name);
}
@AfterReturning(value="execution(* com.pointel.aop.AopTest.beforeAspect(..))")
public void beforeAdvicing(JoinPoint joinPoint,Object result){
String name = joinPoint.getSignature().getName();
System.out.println("Name of the method : "+name);
System.out.println("Method returned value is : " + result);
}
您的 Java 类将是,
package com.pointel.aop;
public class AopTest {
public String beforeAspect( ) {
return "I am a AopTest";
}
}
就是这样。希望它有所帮助。