gpt4 book ai didi

Spring Boot 在启动时抛出 BeanCreationException

转载 作者:行者123 更新时间:2023-11-28 22:53:24 29 4
gpt4 key购买 nike

我已经开始将 Spring MVC/Spring Web Tomcat 应用程序迁移到 Spring Boot。目前我正在将 xml 配置文件迁移到 java 配置。

当我尝试通过 mvn spring-boot:run 启动我的应用程序时,出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.faz.osc.MultitenancyService net.faz.osc.MessageSource.multitenancyService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multitenancyServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.http.HttpServletRequest net.faz.osc.MultitenancyServiceImpl.request; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.initMessageSource(AbstractApplicationContext.java:626)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:468)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:969)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:958)
at net.faz.osc.Application.main(Application.java:21)
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:497)
at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:423)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.faz.osc.MultitenancyService net.faz.osc.MessageSource.multitenancyService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multitenancyServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.http.HttpServletRequest net.faz.osc.MultitenancyServiceImpl.request; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 21 common frames omitted

导致错误的代码片段是

@Service
public class MultitenancyServiceImpl implements MultitenancyService {

public static final String MANDANT_KONNTE_NICHT_UEBER_SERVERNAMEN_ERKANNT_WERDEN = "Mandant konnte nicht über Servernamen erkannt werden.";

@Value("#{'${server.names.faz}'.split(',')}")
private List<String> fazServerNames;

@Value("#{'${server.names.rundschau}'.split(',')}")
private List<String> rundschauServerNames;

@Value("${vkorg.faz}")
private String fazVkorg;

@Value("${vkorg.rundschau}")
private String rundschauVkorg;

@Autowired(required = true)
private HttpServletRequest request;

...

在研究我刚刚发现的网络时, Autowiring HttpServletRequest 应该总是可以正常工作......

在迁移到基于 java 的配置之前,应用程序作为放置在 tomcat 中的 WAR 文件运行良好。

有什么提示我遗漏了什么吗?如果需要更多信息,请告诉我,我将提供相关代码。

韩国哈比卜

最佳答案

由于 HttpServletRequest 是请求范围的 bean,您只能将其注入(inject)到请求对象中;示例 @Controller 类。

因此将 @Autowired(required = true) private HttpServletRequest request; 移动到 Controller 类,并通过 setter 将引用传递给 @Service 类。

关于Spring Boot 在启动时抛出 BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com