2015-04-06 73 views
0

我正在使用Spring 4 AOP,並且我創建的方面永遠不會被調用,我無法弄清楚爲什麼會這樣。你看,我有這樣的客戶端類:Spring 4 AOP方面永遠不會被調用?

package com.example.aspects; 

public class Client { 


    public void talk(){ 

    } 
} 

而且我的方面: 包com.example.aspects;

import org.aspectj.lang.JoinPoint; 
@Aspect 
public class AspectTest { 

    @Before("execution(* com.example.aspects.Client.talk(..))") 
    public void doBefore(JoinPoint joinPoint) { 
     System.out.println("***AspectJ*** DoBefore() is running!! intercepted : " + joinPoint.getSignature().getName()); 
    } 

} 

我的配置文件:

package com.example.aspects; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.EnableAspectJAutoProxy; 

@Configuration 
@EnableAspectJAutoProxy 
public class Config { 

    @Bean 
    public Client client(){ 
     return new Client(); 
    } 

} 

最後,去應用

public class App { 
    public static void main(String[] args) { 
     AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(
       Config.class); 
     Client client = (Client) appContext.getBean("client"); 
     client.talk(); 
    } 
} 

通過這種方式,我從來沒有通過AspectTest doBefore()方法, 「截獲」。你有什麼想法嗎?問候

回答

2

您從未註冊過您的@Aspect。添加相應的bean

@Bean 
public AspectTest aspect() { 
    return new AspectTest(); 
} 

您也可以讓你的類型@Component並在@Configuration類添加適當的@ComponentScan

+0

我不相信我沒有看到它。這太容易了。謝謝! – jscherman

相關問題