【问题标题】:Spring Boot throws BeanCreationException on startupSpring Boot 在启动时抛出 BeanCreationException
【发布时间】:2015-11-13 09:09:27
【问题描述】:

我已经开始将 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

导致错误的代码sn-p是

@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;

...

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

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

任何提示我错过了什么?如果需要更多信息,请告诉我,我会提供相关代码。

韩语 哈比布

【问题讨论】:

  • 你能粘贴 Spring 的 Java 配置文件吗?

标签: spring maven spring-mvc tomcat spring-boot


【解决方案1】:

由于HttpServletRequest是request scoped bean,你只能将它注入到request对象中;例如@Controller 类。

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

【讨论】:

  • 为了补充这个答案,我猜你之前通过某种 XML 配置控制了该类的创建,现在它被引导自动拾取。也许您应该禁用组件扫描?将 @SpringBootApplication 替换为 @EnableAutoConfiguration
  • 嘿,这正是它以前的工作方式。更改了注释,现在应用程序启动了。但不幸的是,现在我在加载页面时收到了一个基本的身份验证请求,而不是显示正常页面。有什么想法吗?
  • 嗨,你有这个参考的链接吗? @Lovababu 说控制器注释是请求对象类还是您只是从源代码中读取?但是谢谢!
【解决方案2】:

我通过删除 required=true 解决了这个问题。现在它起作用了。 感谢到目前为止的提示!

【讨论】:

  • 您设置为@Autowired(required=false) 了吗?如果是,这可能会导致一些 NPE,因为 Spring 可能不会为您注入它,所以最好检查空值
  • 奇怪,未设置时默认为true,但如果有效:D
  • hmmm 我应该更准确地阅读我自己的代码......我不只是删除了所需的属性,我评论了整个 Autowired 注释......只要我启动应用程序就可以工作...... . 但是在执行请求时 HttpServletRequest 为空......所以我继续寻找解决方案......
猜你喜欢
  • 2020-08-16
  • 1970-01-01
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多