이전 글을 작성하다가 문든 들은 생각(@Aspect 가 붙은 클래스들끼리 상속을 하면 포인트컷과 어드바이스를 상속할 수 있는가?)을 실험해봤습니다.

//CinamaAspect
@Aspect
public class CinemaAspect {

    @Pointcut(“execution(* sellTicket(..))”)
    public void sellTicketPointcut() {
    }

    @Before(“aop.newStyle.aspect.ChildAspect.sellTicketPointcut()”)
    public void weblcomeAdvice(){
        System.out.println(“안녕하세요. 어떤 영화를 보시겠습니까?”);
    }
}

//ChildAspect
@Aspect
public class ChildAspect extends CinemaAspect {

}

그리고 이전에 테스트를 돌리면 다음과 같은 에러메시지를 볼 수 있습니다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘혜인’ defined in class path resource [aop/newStyle/watchingMovieConfiguration.xml]: Cannot resolve reference to bean ‘KCcard’ while setting bean property ‘card’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘KCcard’ defined in class path resource [aop/newStyle/watchingMovieConfiguration.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: aop.newStyle.aspect.ChildAspect cannot extend concrete aspect aop.newStyle.aspect.CinemaAspect

concrete aspect를 상속할 수 없다고 하길래 인터페이스를 만들어서 실험해 봤습니다.

//AbstractAspect
@Aspect
public interface AbstractAspect {
    @Pointcut(“execution(* sellTicket(..))”)
    public void sellTicketPointcut();
}

//ChildAspect
@Aspect
public class ChildAspect implements AbstractAspect {

    /* (non-Javadoc)
     * @see aop.newStyle.aspect.AbstractAspect#sellTicketPointcut()
     */
    public void sellTicketPointcut() {
    }
}

//설정파일
    <bean id=”cinemaAspect” class=”aop.newStyle.aspect.CinemaAspect” />
    <bean id=”childAspect” class=”aop.newStyle.aspect.ChildAspect” />
    <bean id=”abstractAspect” class=”aop.newStyle.aspect.AbstractAspect” abstract=”true”/>

하지만 역시 에러가 납니다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘혜인’ defined in class path resource [aop/newStyle/watchingMovieConfiguration.xml]: Cannot resolve reference to bean ‘KCcard’ while setting bean property ‘card’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘KCcard’ defined in class path resource [aop/newStyle/watchingMovieConfiguration.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can’t make static reference to abstract pointcut
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘KCcard’ defined in class path resource [aop/newStyle/watchingMovieConfiguration.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can’t make static reference to abstract pointcut
Caused by: java.lang.IllegalArgumentException: error at ::0 can’t make static reference to abstract pointcut…

흠.. ChildAspect에 있는 @Aspect 를 제거하고 다시 상속을 사용해봤습니다.

//CinemaAspect
@Aspect
public class CinemaAspect {

    @Pointcut(“execution(* sellTicket(..))”)
    public void sellTicketPointcut() {
    }

    @Before(“aop.newStyle.aspect.ChildAspect.sellTicketPointcut()”)
    public void weblcomeAdvice(){
        System.out.println(“안녕하세요. 어떤 영화를 보시겠습니까?”);
    }
}

//ChildAspect
public class ChildAspect extends CinemaAspect {

}

이렇게 하니까 제대로 동작을 합니다. 포인트컷은 상속이 되는 걸 확인했습니다. 하지만..advice를 ChileAspect로 옮겨 놓으니까 제대로 동작하지 않네요. 당연히…안되죠. ChileAspect는 이름만 Aspect일뿐 일반 클래스기 때문에 @Advice가 붙어 있다 하더라도 인식하지 않습니다.