추상(abstract) 애스펙트 클래스를 상속받아 포인트컷 메소드나 어드바이스 메소드를 오버라이딩하는 경우에 무엇이 적용되는가에 대해서 정리합니다. (참고로 애스펙트는 abstract 애스펙트만 상속받을 수 있습니다)


포인트컷 메소드 오버라이딩


추상 애스펙트에 정의된 (non-abstract) 포인트컷 메소드를 하위 애스펙트에서 오버라이딩하는 경우


(1) 포인트컷을 재정의하지 않은 경우

=> 추상 애스펙트의 포인트컷이 적용됨


(2) 포인트컷을 재정의한 경우 

=> 하위 애스펙트의 포인트컷이 적용됨


결론 : 자식 애스펙트에서 부모 애스펙트에 정의된 포인트컷을 재정의할 수 있다.



어드바이스 메소드 오버라이딩


추상 애스펙트에 정의된 어드바이스 메소드를 하위 애스펙트에서 오버라이딩하는 경우


(1) 어드바이스를 재정의하지 않은 경우

=> 추상 애스펙트의 어드바이스가 적용되지만 동작(메소드)는 하위 애스펙트의 것이 적용됨


(2) 어드바이스도 재정의한 경우

=> 추상 애스펙트의 어드바이스도 적용되고 하위 애스펙트의 어드바이스도 적용됨. 동작(메소드)은 하위 애스펙트의 것이 적용됨


결론 : 자식 애스펙트에서는 부모 애스펙트에 정의된 어드바이스의 동작(메소드 내용)은 재정의할 수 있지만,

언제(before, after 등) 어드바이스를 적용할지는 수정할 수 없다(무조건 새로운 적용 위치가 추가됨).


Posted by 에코지오
,

포인트컷 재사용


공통 포인트컷만 모아놓은 애스펙트 작성를 작성합니다. 예를들어 MyPointcuts.

다른 애스펙트에서는 MyPointcuts.somePointcut() 처럼 static하게 액세스하여 참조합니다.


@Aspect

public class MyPointcuts {

 @Pointcut("within(my..On*Listener+) && execution(!private !static * on*(..) throws !Exception)")

 public void eventListenerMethods() {}

}


@Aspect

public class MyAspect {

    @Around("my.MyPointcuts.eventListenerMethods()")

    public Object handleListenerException(final ProceedingJoinPoint pjp) {

        try {

            return pjp.proceed();

        } catch (final Throwable e) {

            exceptionHandler.handle(e);

            return null;

        }

    }

}



어드바이스 재사용

abstract 포인트컷을 만들고 그 포인트컷을 사용하는 어드바이스를 작성합니다. 애스펙트를 abstract로 선언해야 합니다(여전히 @Aspect 어노테이션 필요함).


@Aspect

public abstract class MyAspect {

 @Pointcut("")

 protected abstract void toOverridePointcut(); 


 @After("toOverridePointcut()")

 public void myAdvice() {

    ... ...

 }

 ...

}


하위 애스펙트에서 abstract 포인트컷을 구현합니다.


@Aspect

public class YourAspect extends MyAspect {

 @Pointcut("within(android.app.Activity+) && execution(void onCreate())")

 protected void toOverridePointcut() {}


 ... ...

}


Posted by 에코지오
,

RoboGuice 사용환경에서 AspectJ의 aspect 클래스에 모듈을 injection하는 방법입니다.



방법1. static 방식으로 injection


(1) 애스펙트 클래스에서 static 멤버변수로 선언

주입할 모듈을 static 멤버변수로 선언합니다.


 @Inject protected static ContextScopedProvider<T> tProvider; // T는 @ContextSingleton이어야함

 @Inject protected static ExceptionHandler exceptionHandler;//Singleton


(2) Guice 모듈 설정에서 static injection 처리

configure() 메소드에서 requestStaticInjection 메소드를 통해 주입합니다.


requestStaticInjection(ExceptionHandlingAspect.class);


그러나 이 방법은 권장하지 않습니다. Guice에서도 static injection은 deprecated될 것이라고 합니다.

http://code.google.com/p/google-guice/wiki/AvoidStaticState



방법 2. aspect 객체를 구하여 injection


aspect는 AspectJ에 의해 인스턴스화되며 우리가 직접 생성할 수 없습니다. 대부분의 경우 aspect는 싱글턴 객체이며, AspectJ는 싱글턴 aspect 객체를 참조할 수 있는 Aspects.aspectOf() 메소드를 제공합니다.


(1) 주입할 모듈을 애스펙트 클래스에 보통의 멤버변수로 선언


 @Inject protected ExceptionHandler exceptionHandler;


(2) Guice 모듈 설정에서 requestInjection을 통해 injection 처리


requestInjection(org.aspectj.lang.Aspects.aspectOf(ExceptionHandlingAspect.class));


참고:

Posted by 에코지오
,