2017-10-01 1 views
0

내 및 대상 pointcut 지정자 사이에 다른 점을 이해할 수 없습니다. 이 예에서 봐 :Spring AOP, 대상 지정자 내

@Component 
public interface Icamera { 
    public void snap() throws Exception; 
} 

@Component 
class Camera implements Icamera{ 
    public void snap() throws Exception { 
     System.out.println("SNAP!"); 

    } 
} 


@Aspect 
@Component 
public class Logger { 

**@Pointcut("within(com.test.aop.Icamera)")** 
public void cameraSnap() { 
} 

@Before("cameraSnap()") 
public void beforeAdvice() { 
    System.out.println("Before advice ..."); 
} 

@After("cameraSnap()") 
public void afterAdvice() { 
    System.out.println("After advice ..."); 
} 

public static void main(String[] args) throws Exception { 
    ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 
    Icamera camera=context.getBean(Icamera.class); 
    camera.snap(); 
} 

}

출력은 다음과 같습니다 SNAP! 하지만 내부 대신 대상을 사용하면 출력은 다음과 같습니다. 사전 안내 ... 스냅! 통보 후 ...

답변

0

봄 문서는 특히 명확하지 않다 그러나 그들은 전체 지원의 AspectJ 포인트 컷 타입의 참조 제공 : https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-pointcuts 귀하의 예를

의 경우

내 - 일치하는 한계를 특정 유형 내의 포인트를 조인 할 수 있습니다 (Spring AOP를 사용할 때 일치하는 유형 내에서 선언 된 메소드를 실행하는 것).

따라서 메서드 호출을받는 실제 @Component의 형식은 within의 선택기와 일치해야합니다. 당신이 빈 클래스 이름을 지정하는 경우 통화를 차단합니다

@Pointcut("within(com.test.aop.Camera)") 

패키지 선택

@Pointcut("within(com.test.aop.*)") 

또는 하위 패키지를 선택 일부 부모 패키지

@Pointcut("within(com.test..*)") 

대상 - 한계를 조인 포인트 (Spring AOP를 사용할 때 메소드 실행)와 일치하는 대상 오브젝트 (appl 가스화 개체 프록시를) 지정된 형태의 문서에서 알 수 있듯이

의 인스턴스 방법은 <beanClass> instanceof <targetType>true로 평가 모든 콩 호출에 적용됩니다.