2017-10-01 7 views
0

対以内に私は内とターゲットポイントカット指定の間で異なる理解できませんでした。 例を見て:春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! しかし、あなたは内の代わりにターゲットを使用する場合、出力は次のとおりです。 アドバイスする前に... SNAP!アドバイスの後 ...

答えて

0

春のドキュメントは特に明確ではないが、彼らは、サポートAspectJのポイントカットタイプの完全なリファレンスを提供します。内https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-pointcuts

あなたの例の場合

- マッチング限界特定の種類の

(スプリングAOPを使用する場合、一致する型内に宣言方法の単に実行)内の点を結合します

したがって、メソッド呼び出しを受け取る実際の@Componentのタイプは、withinのセレクタと一致する必要があります。あなたがBeanクラス名を指定する場合は、コールを傍受します

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

パッケージセレクタ

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

またはサブパッケージセレクタといくつかの親パッケージ

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

ターゲット - 限界を結合点のマッチング(Spring AOPを使用する場合のメソッドの実行)対象オブジェクト(applプロキシさicationオブジェクト)ドキュメントは<beanClass> instanceof <targetType>trueに評価されるすべてのBeanのメソッド呼び出しに適用されるとおり、所与のタイプ

のインスタンスです。

関連する問題