2013-04-12 26 views
0

所以,我一直在試圖使用Spring AOP,但是一旦我開始使用自定義方法註釋,AOP就停止工作。自定義註釋未被Spring AOP檢測到

這裏是註釋:

package com.test.annotations; 

import java.lang.annotation.ElementType; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface Performance { 

} 

的看點:

package com.test.aspects; 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 


@Component 
@Aspect 
public class Audience { 
    @Pointcut("@annotation(com.test.annotations.Performance)") 
    public void performance() { 
    } 

    @Around("performance()") 
    public void beforePerformance(ProceedingJoinPoint jointPoint) throws Throwable{ 
     System.out.println("The audience is getting ready for the show"); 

     jointPoint.proceed(); 

     System.out.println("The show is over, audience's leaving"); 
    } 

} 

的使用類定製註釋:

package com.test.performers; 

import com.test.annotations.Performance; 
import com.test.exceptions.PerformanceException; 

public interface Performer { 
    @Performance 
    void perform() throws PerformanceException; 
} 

最後,主法的相關部分。

Performer kenny = (Performer) context.getBean("guitarist"); 
kenny.perform(); 

吉他手類正在實現表演者界面。

我一直在四處尋找幾個小時,我看不到我做錯了什麼。謝謝 !

回答

0

註釋中沒有繼承。在Guitaritst類中,當覆蓋perform()方法時,也應該註釋它。

+0

這解決了它。有沒有辦法模仿這種行爲? –

+0

不是我所知道的.. –