2015-04-01 73 views
1

此問題可能很簡單,但我無法找到解決方案。 我正在使用Spring AOP,目前我只是試圖獲取對由框架本身創建的代理對象的引用。 我接着this線程提供的說明,但我仍然得到以下異常:線程「main」 java.lang.IllegalStateException無法訪問Spring AOP中的代理對象

異常:找不到當前代理:設置「exposeProxy」屬性上奉勸「真正'來使其可用。

下面是我的springContext.xml所需的條目:

<aop:aspectj-autoproxy expose-proxy="true"/> 

這是我的定義方面:

@Aspect 
public class DynamicAspect { 


@Pointcut("execution(* addition.aop.Actor.play(..))") 
public void play() { 

} 

@Before("play()") 
public void directorGivesInstructions() { 
    System.out.println("Director: Light, camera, action!"); 
} 

主法,一切得到測試:

public static void main(String[] args) {  
ApplicationContext context = new ClassPathXmlApplicationContext(...); 
Actor actor = (Actor) context.getBean("actor"); 
actor.play(); 
} 

這是Actor類中引發異常的方法:

public void play() { 
    AopContext.currentProxy(); 
    System.out.println("Actor starts playing his role..."); 
} 

該方面編織並正確調用。

非常感謝!

回答

0

最後,我發現自己的錯誤。上面的代碼工作正常。但是,將編譯器從ajc交換到javac,我意識到代理展示只能用javac而不是AspectJ編譯器編譯。我希望這會在未來幫助你們中的一些人!