Annotation(@Aspect, @Component)을 이용해 AOP를 적용시켜본 예제임.

SpringFramework에서 제공하는 Annotation(@Aspect, @Component)을 이용하기 위해 XML에 기술

<context:component-scan base-package="com.test.aop" />

AOP에선 AOP가 적용될 클래스 및 메소드의 패턴이 표현되는 Pointcut이 가장 중요하다고 함.

spring-test.jar를 이용해 테스트를 해보면 성공!

예외 정리


1. @ContextConfiguration에서 Locations속성을 설정하지 않으면 같은 패키지에 GreetingTest-context.xml을 디폴트값으로 참조함. 없을 경우 IO예외가 발생함.(FileNotFoundException)

2. @ContextConfiguration에서 정의한 GreetingTest-context.xml의 classpath 경로가 틀리거나 다른 이름일 경우(예-Beans.xml) java.lang.NoSuchMethodError: org.junit.internal.runners.MethodValidator.<init>(Lorg/junit/internal/runners/TestClass;) 발생함.

3. Eclipse3.3에 포함되어있는 JUnit의 경우 버전이 4.1이고 이를 사용하면 "@RunWith(SpringJUnit4ClassRunner.class)"부분에서 컴파일 에러가 발생함.
에러 내용 : Type mismatch: cannot convert from Classto Class

4.
Pointcut이 적용이 안될 경우
- AspectJ문법이 틀렸거나, JDK버전문제(이 부분이 조금 애매하긴 함. 구글링중 JDK6이 문제를 일으킨다는 글이 많았는데 결국 JDK6에서도 테스트 성공!)
@Before("greetingInterceptor()"), @AfterReturning("greetingInterceptor()") 부분을
@Before("execution(* * ..Greeting.*(..))"),
@AfterReturning("execution(* * ..Greeting.*(..))")
로 수정 후 테스트 시도.
에러 내용 : Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut greeting

5. @Autowired 이용시 Interface 타입을 이용.
구현 클래스를 타입으로 이용했을 경우 에러 내용
Unsatisfied dependency of type [class com.test.aop.GreetingImpl]: expected at least 1 matching bean

소스 다운 받기 및 설치.
http://kekedie.tistory.com/68


다운로드 받을 파일을 압축을 풀고 Eclipse의 workspace에 넣은 후 메뉴탭에서
File > Import > General > Existing Projects into Workspace 하면 됨.