Spring 4 AOP 方面永远不会被调用?
Spring 4 AOP aspect is never being called?
我正在使用 Spring 4 AOP,我创建的方面从未被调用,我无法弄清楚为什么会这样。看,我有这个客户 class:
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();
}
}
最后,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() 方法获得 "intercepted"。你知道发生了什么事吗?问候
您从未注册过 @Aspect
。添加对应的bean
@Bean
public AspectTest aspect() {
return new AspectTest();
}
您也可以将您的类型设置为 @Component
并在您的 @Configuration
class.
中添加适当的 @ComponentScan
我正在使用 Spring 4 AOP,我创建的方面从未被调用,我无法弄清楚为什么会这样。看,我有这个客户 class:
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();
}
}
最后,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() 方法获得 "intercepted"。你知道发生了什么事吗?问候
您从未注册过 @Aspect
。添加对应的bean
@Bean
public AspectTest aspect() {
return new AspectTest();
}
您也可以将您的类型设置为 @Component
并在您的 @Configuration
class.
@ComponentScan