Spring AOP 阻塞 RestController
Spring AOP blocks RestController
我尝试学习Spring AOP。我在 IDEA 中创建了简单的 spring 引导项目。
Service.java
package com.example.demo.service;
//imports..
public interface Service {
public DataEntity getData();
}
ServiceImpl.java
package com.example.demo.service;
//imports..
@RestController("service")
public class ServiceImpl implements Service {
@RequestMapping(value="/test", method= RequestMethod.GET)
public DataEntity getData() {
DataEntity data = new DataEntity();
data.setData("SomeString");
return data;
}
}
ServiceCallingAspect.java
package com.example.demo.aspects;
//imports..
@Aspect
@EnableAspectJAutoProxy
@Component
public class ServiceCallingAspect {
private Log log = LogFactory.getLog(ServiceCallingAspect.class);
@AfterReturning("execution(public * com.example.demo.service.*.*(..))")
public void logBeforeRestCall(JoinPoint pjp) throws Throwable {
log.info(" POST REST call " + pjp);
}
}
DemoApplication.java
package com.example.demo;
//..
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
所以当我尝试在 http://localhost:8080/test
上调用我的休息服务时,我得到了类似的东西。
{
"timestamp": 1514109432038,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/test"
}
当我禁用我的方面(只需评论 ServiceCallingAspect.java
中的所有注释)时,该服务运行完美。你能告诉我哪里错了吗?
将 @EnableAspectJAutoProxy
更改为 @EnableAspectJAutoProxy(proxyTargetClass=true)
。
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass=true)
@Component
public class ServiceCallingAspect {
.....
}
我尝试学习Spring AOP。我在 IDEA 中创建了简单的 spring 引导项目。
Service.java
package com.example.demo.service;
//imports..
public interface Service {
public DataEntity getData();
}
ServiceImpl.java
package com.example.demo.service;
//imports..
@RestController("service")
public class ServiceImpl implements Service {
@RequestMapping(value="/test", method= RequestMethod.GET)
public DataEntity getData() {
DataEntity data = new DataEntity();
data.setData("SomeString");
return data;
}
}
ServiceCallingAspect.java
package com.example.demo.aspects;
//imports..
@Aspect
@EnableAspectJAutoProxy
@Component
public class ServiceCallingAspect {
private Log log = LogFactory.getLog(ServiceCallingAspect.class);
@AfterReturning("execution(public * com.example.demo.service.*.*(..))")
public void logBeforeRestCall(JoinPoint pjp) throws Throwable {
log.info(" POST REST call " + pjp);
}
}
DemoApplication.java
package com.example.demo;
//..
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
所以当我尝试在 http://localhost:8080/test
上调用我的休息服务时,我得到了类似的东西。
{
"timestamp": 1514109432038,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/test"
}
当我禁用我的方面(只需评论 ServiceCallingAspect.java
中的所有注释)时,该服务运行完美。你能告诉我哪里错了吗?
将 @EnableAspectJAutoProxy
更改为 @EnableAspectJAutoProxy(proxyTargetClass=true)
。
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass=true)
@Component
public class ServiceCallingAspect {
.....
}