2

我可以从AspectJ 中带有注释@Before的方法返回吗?

@Before
public void simpleAdvice(JoinPoin joinPoint) {
    if (smth == null)
    /* return for method, which annotated */
}

如果我的问题不完全,请再问我一个了解详情。

4

2 回答 2

1

您可以使用@Before , @After , @AfterReturning, @AfterThrowing,定义方法@Around。但是您的课程可以使用@Aspect.

您还需要定义pointcutand 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";
    }
}

就是这样。希望它有所帮助。

于 2013-09-16T07:15:18.527 回答
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()");
    }
}
于 2013-09-16T07:06:55.313 回答