[英]How to solve problem of bean proxy creation? Java, Spring
我遇到了一个问题,即无法为 bean 创建代理。 我使用 java、Spring。 代码如下。 我已经解决了这个问题好几天了,仍然不知道出了什么问题。
@SpringBootApplication
@EnableScheduling
@EnableCaching
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
我从另一个服务调用我的服务方法:
@Transactional
public void updateState(List<Long> ids, ResourceState state) {
ResourceService proxy = (ResourceService) AopContext.currentProxy();
proxy.updateStateBySystem(Lists.newArrayList(repository.findAllById(ids)), state);
}
我在同一服务中调用此方法:
@Async
@Transactional
public void purchaseForOrder(Order order) {
List<Resource> list = getResourcesForItem(order.getId(), null);
updateState(list.stream().map(ApplicationModel::getId).collect(Collectors.toList()), ResourceState.PURCHASING);
}
我收到以下错误消息:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available, and ensure that AopContext.currentProxy() is invoked in the same thread as the AOP invocation context.
at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:69)
at ru.sberbank.irpm.ApiApplication.main(ApiApplication.java:26)
... 5 more
实际上
非常感谢大家对我的任务感兴趣并愿意提供帮助。 我通过在服务本身中显式调用服务解决了这个问题。
ResourceService proxy = ((ResourceService) applicationContext.getBean("resourceService")).getResourceServiceCurrentProxy();
并在服务本身中创建一个调用代理的方法。
public ResourceService getResourceServiceCurrentProxy() {
return (ResourceService) AopContext.currentProxy();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.