方面没有得到执行
Aspect not getting executed
我的 AspectConfig
@Configuration
@EnableAspectJAutoProxy
public class LoggingAspectConfig {
@Bean
public SampleRestService restService() {
return new SampleRestService();
}
@Bean
public ServiceMonitor serviceMonitor() {
return new ServiceMonitor();
}
}
我的看点
@Aspect
@Component
public class ServiceMonitor {
@Pointcut("@annotation(com.web.rest.logging.Monitor)")
public void requestMapping() {}
@Before("requestMapping()")
public void logServiceStart(JoinPoint joinPoint) {
System.out.println("Start: " + joinPoint);
System.out.println(joinPoint.getSignature());
System.out.println(joinPoint.getSignature().getName());
}
}
我的示例服务
@Service
public class SampleRestService {
@Monitor
public static void getParams(){
String url = "<sample url>";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response2 = restTemplate.exchange( url, HttpMethod.GET, entity , String.class );
System.err.println(response2.getBody());
}
我的批注
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Monitor {
}
我正在用这个 getParams 从一个用 @Component 注释的控制器调用
如果我遗漏了什么,请告诉我,
我需要添加更多配置吗?还是切入点表达式错误。
我有以下罐子
aspectjweaver-1.8.9.jar
aspectjrt-1.8.9.jar
spring-aop-4.2.0.RELEASE.jar
您正在使用 Spring AOP 而不是 AspectJ,如配置注释 @EnableAspectJAutoProxy
所示。 Spring AOP 不等同于AspectJ 编织。 Spring AOP 通过代理您的 spring 托管 bean 来工作,因此它仅适用于 spring beans,具有基于代理的 AOP 与字节码编织的所有限制。您的候选方法 getParams
是静态方法,因此它不是 Spring AOP 的候选方法。使用正常的 AspectJ(编译时织入或加载时织入)或者如果您打算坚持使用 Spring AOP,则从方法中删除 static
关键字。请参阅 this answer 以获取更多参考。
我的 AspectConfig
@Configuration
@EnableAspectJAutoProxy
public class LoggingAspectConfig {
@Bean
public SampleRestService restService() {
return new SampleRestService();
}
@Bean
public ServiceMonitor serviceMonitor() {
return new ServiceMonitor();
}
}
我的看点
@Aspect
@Component
public class ServiceMonitor {
@Pointcut("@annotation(com.web.rest.logging.Monitor)")
public void requestMapping() {}
@Before("requestMapping()")
public void logServiceStart(JoinPoint joinPoint) {
System.out.println("Start: " + joinPoint);
System.out.println(joinPoint.getSignature());
System.out.println(joinPoint.getSignature().getName());
}
}
我的示例服务
@Service
public class SampleRestService {
@Monitor
public static void getParams(){
String url = "<sample url>";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response2 = restTemplate.exchange( url, HttpMethod.GET, entity , String.class );
System.err.println(response2.getBody());
}
我的批注
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Monitor {
}
我正在用这个 getParams 从一个用 @Component 注释的控制器调用
如果我遗漏了什么,请告诉我,
我需要添加更多配置吗?还是切入点表达式错误。
我有以下罐子
aspectjweaver-1.8.9.jar aspectjrt-1.8.9.jar spring-aop-4.2.0.RELEASE.jar
您正在使用 Spring AOP 而不是 AspectJ,如配置注释 @EnableAspectJAutoProxy
所示。 Spring AOP 不等同于AspectJ 编织。 Spring AOP 通过代理您的 spring 托管 bean 来工作,因此它仅适用于 spring beans,具有基于代理的 AOP 与字节码编织的所有限制。您的候选方法 getParams
是静态方法,因此它不是 Spring AOP 的候选方法。使用正常的 AspectJ(编译时织入或加载时织入)或者如果您打算坚持使用 Spring AOP,则从方法中删除 static
关键字。请参阅 this answer 以获取更多参考。