2015-09-30 53 views
0

我旁邊的接口和實現:的Spring AOP:創建切入點其檢查父類註釋

@Step 
public interface TestAopComp { 
    void test(); 
} 

@Component 
public class TestAopCompImpl implements TestAopComp{ 
    public void test(){ 
     System.out.println("test"); 
    } 
} 

我需要的類中的所有方法,擴展類與註釋@Step攔截執行。請幫我寫切入點。

例如筆者使用的下一個切入點爲類攔截方法,它通過@Step註釋:

@Pointcut("@within(Step)") 

但它不工作,如果我只編超類

回答

0

我調查的問題。如果您使用從類的擴展(它可能是抽象的)和註釋宣佈爲繼承比我的切入點將工作,但它不適用於實現的接口。

下一個例子將是工作,但它並不適用於註釋上實現的接口工作:

@Step 
public abstract class TestAopComp { 
    public abstract void test(); 
} 

@Component 
public class TestAopCompImpl extends TestAopComp{ 
    public void test(){ 
     System.out.println("test"); 
    } 
} 

@Inherited 
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.TYPE) 
public @interface Step { 
} 

@Component 
@Aspect 
public class Aspect { 
    @Pointcut("@within(Step)") 
    public void stepClass(){} 

    @Around("stepClass()") 
    public void stepAround(ProceedingJoinPoint pjp){ 
     System.out.println("before"); 
     try { 
      pjp.proceed(pjp.getArgs()); 
     } catch (Throwable throwable) { 
      throwable.printStackTrace(); 
     } 
     System.out.println("after"); 
    } 
}