我正在使用Spring 4 AOP,我创建的方面永远不会被调用,我无法弄清楚为什么会这样。看,我有这个客户端类:
package com.example.aspects;
public class Client {
public void talk(){
}
}
我的方面: package 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();
}
}
最后,de app
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()方法“拦截”。你知道发生了什么吗?此致
答案 0 :(得分:2)
您从未注册过@Aspect
。添加相应的bean
@Bean
public AspectTest aspect() {
return new AspectTest();
}
您还可以将类型设为@Component
,并在@ComponentScan
课程中添加适当的@Configuration
。