方面没有被执行

时间:2016-10-17 20:05:13

标签: java spring spring-aop

我的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 {

}

我用来自@Component

注释的Controller调用此getParams

如果我错过了什么,请告诉我,

我是否需要添加更多配置?或者是切入点表达错误。

我有以下罐子

aspectjweaver-1.8.9.jar aspectjrt-1.8.9.jar 弹簧AOP-4.2.0.RELEASE.jar

1 个答案:

答案 0 :(得分:2)

您正在使用Spring AOP而不是AspectJ,如配置注释@EnableAspectJAutoProxy所示。 Spring AOP不等同于AspectJ编织。 Spring AOP通过代理您的spring托管bean来工作,因此它只适用于spring bean,具有基于代理的AOP与字节码编织的所有限制。候选方法getParams是一种静态方法,因此它不是Spring AOP的候选方法。如果您打算坚持使用Spring AOP,请使用正常的AspectJ(编译时编织或加载时编织)或从方法中删除static关键字。有关详细信息,请参阅this answer