【发布时间】:2017-02-26 22:21:39
【问题描述】:
我的 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 注释的控制器中使用此 getParams 调用
如果我遗漏了什么,请告诉我,
我需要添加更多配置吗?还是切入点表达式错误。
我有以下罐子
aspectjweaver-1.8.9.jar aspectjrt-1.8.9.jar spring-aop-4.2.0.RELEASE.jar
【问题讨论】:
标签: java spring spring-aop