2012-08-01 2 views
8

제 연구에서 저는 AspectJ를 사용하는 두 가지 방법이 있음을 압니다. 먼저 A.aj 클래스를 만들고 @Aspect이라는 A.java에 주석을 추가하여 두 번째 클래스를 만듭니다.AspectJ : 두 종류의 자습서

나는 특히

@After("call(void fooMethod())") 
@Around("call(void sendAndReceive())") 
@Before("execution(String greeting(..)) && args(context)") 

같은 라인에 대해, 두 번째 종류의 좋은 튜토리얼을 찾고 있었다 그러나 나는 그들이 호출 방법을 모르겠어요.

자습서를 추천 해 주시겠습니까?

답변

1

주석과 XML 방법 :

주석 방법 : 최소한의 XML 구성 파일 :

<!-- Enable autoproxy to pick up all Java files tagged as @Aspect behave like Aspects --> 
<aspectj-autoproxy/> 
<!-- define bean --> 
<!-- Note: MyUselessAspect.java should exist and this class must be tagged as @Aspect --> 
<bean id="myUselessAspect" class="...MyUselessAspect" /> 

XML 방법 : 최소한의 XML 구성 :

<aop:config> 
    <aop:aspect ref="myUselessAspect"> 
     <!-- this point-cut picks all methods of any return type, from any package/class with any number of Parameters --> 
    <aop:before method="doSomethingBeforeMethodCall" pointcut="execution(* *.*(..))"/> 
    <aop:after method="doSomethingAfterMethodCall" pointcut="execution(* *.*(..))"/> 
    </aop:aspect>   
</aop:config> 
<!-- No need to Annotate this java Class as @Aspect. Neither you need to define any 
Point-cuts or Advices in the Java file. The <aop:config> tag takes care of everything --> 
<bean id="myUselessAspect" class="...MyUselessAspect"></bean> 

코드를 변경할 필요가 없습니다.

전 필수 : ​​ AOP의 네임 스페이스는 AspectJ를를 사용하는 사람들의 이익을 위해 주목 XML 파일

+0

에 존재하지만, 봄에 익숙하지 있어야합니다 - 여기 크람의 대답은 봄의 구성 측면을 구성하는 방법을 해결하고 파일. 또한 XML 구성 파일을 통해 aspect를 정의하는 * 세 번째 방법을 제공합니다. Spring이 아닌 AspectJ에서 직접 프로그래밍 할 때 AspectJ 설정 파일 (일반적으로 'aop.xml')에서 비슷한 기능을 사용할 수있다. 불행히도, 이것은 원래의 질문을 다루는 것이 아닙니다. –