获取 org.springframework.beans.factory.BeanCreation异常:创建带有名称的



这是我的控制器类。我的代码正在生产中并且正在工作。现在,我在用户故事II上并试图进行更改。它开始失败 自动接线 ,所以我改用新的订单服务,以便可以进行实例化。现在,它失败了,给了我BeanCreationException,所以我添加了PostConstruct初始化方法,但我仍然收到错误。

@RestController
@RequestMapping("/order")
public class OrderController {
private static final Logger log = LoggerFactory.getLogger(OrderController.class);
OrderDetailsService orderDetailsService = new OrderDetailsService();
@PostMapping
public OrderResponse order(@RequestBody(required=false) OrderRequest orderRequest) {
return orderDetailsService.order(orderRequest);
}
@PostConstruct
public void orderControllerinit() {
log.debug("orderDetailsService = " + orderDetailsService.toString());
}  
}

这是错误.


rror starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.]
[2020-06-12 16:59:53.644] [ERROR] [Context:SpringApplication] [] [Application run failed]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderController' defined in file [C:UsersP2932832BPradhanSCIAautomationosm-moduletargetclassescomspectrumsciosmcontrollersOrderController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.spectrum.sci.osm.controllers.OrderController]: Constructor threw exception; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.spectrum.sci.osm.OsmModuleApplication.main(OsmModuleApplication.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.spectrum.sci.osm.controllers.OrderController]: Constructor threw exception; nested exception is java.lang.StackOverflowError
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:216)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312)
... 22 common frames omitted
Caused by: java.lang.StackOverflowError: null
at com.spectrum.sci.osm.service.OrderDetailsService.<init>(OrderDetailsService.java:76)
at com.spectrum.sci.osm.service.CommonService.<init>(CommonService.java:53)
at com.spectrum.sci.osm.service.OrderDetailsService.<init>(OrderDetailsService.java:76)
at com.spectrum.sci.osm.service.CommonService.<init>(CommonService.java:53
org.springframework.beans.factory.BeanCreationException

不是原因,这是堆栈中的最后一个异常,你看到,原因是java.lang.StackOverflowError.

Caused by: java.lang.StackOverflowError: null
at com.spectrum.sci.osm.service.OrderDetailsService.<init>(OrderDetailsService.java:76)
at com.spectrum.sci.osm.service.CommonService.<init>(CommonService.java:53)
at com.spectrum.sci.osm.service.OrderDetailsService.<init>(OrderDetailsService.java:76)
at com.spectrum.sci.osm.service.CommonService.<init>(CommonService.java:53

看起来,OrderDetailsService创建了CommonService,CommonService创建了OrderDetailsService,创建了CommonService等,形成了无限递归,从而导致StackOverflowError

每个类(CommonService.java:53)后面的数字是行号,查找并解析递归。

最新更新