2011-04-13 5 views

답변

13

the following document과 같이 after() returningafter() throwing 어드 바이스를 사용할 수 있습니다. @AspectJ 구문을 사용하는 경우 @AfterReturning@AfterThrowing 주석 (here 샘플을 찾을 수 있음)을 참조하십시오.

5

around() 조언을 사용하면 proceed()을 사용하여 가로 채기 방식 호출의 반환 값을 가져올 수 있습니다. 원하는 경우 메서드에서 반환 한 값을 변경할 수도 있습니다.

public aspect mAspect { 
    pointcut mexec() : execution(* m(..)); 

    int around() : mexec() {  
    // use proceed() to do the computation of the original method 
    int original_return_value = proceed(); 

    // change the return value of m() 
    return original_return_value * 100; 
    } 
} 
6

또한 반환 값을 얻을 수 있습니다 :

public class MyClass { 
    int m() { 
    return 2; 
    } 
} 

것은 당신이 자신의 .aj 파일에 다음과 같은 측면이 있다고 가정 : 예를 들어

, 당신은 클래스 MyClass 내부 방법 m()이 있다고 가정 조언을 회수 한 후 을 사용하십시오.

package com.eos.poc.test; 

public class AOPDemo { 
      public static void main(String[] args) { 
       AOPDemo demo = new AOPDemo(); 
       String result= demo.append("Eclipse", " aspectJ"); 
      } 
      public String append(String s1, String s2) { 
       System.out.println("Executing append method.."); 
       return s1 + s2; 
      } 

} 

받고 반환 값의 정의 측면 :

public aspect DemoAspect { 
    pointcut callDemoAspectPointCut(): 
     call(* com.eos.poc.test.AOPDemo.append(*,*)); 

    after() returning(Object r) :callDemoAspectPointCut(){ 
     System.out.println("Return value: "+r.toString()); // getting return value 

    } 
관련 문제