我已经开始将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;
...
在研究我刚刚发现的网页时,自动装配HttpServletRequest应该始终正常工作......
在迁移到基于java的配置之前,应用程序可以正常工作,因为WAR文件放在tomcat中。
任何暗示我缺少的东西?如果需要进一步的信息,请告诉我,我将提供相关的代码。
KR 哈比卜
答案 0 :(得分:1)
由于HttpServletRequest
是请求范围的bean,因此只能将其注入请求对象;示例@Controller
类。
所以将@Autowired(required = true) private HttpServletRequest request;
移动到控制器类,并通过setter将引用传递给@Service
类。
答案 1 :(得分:0)
我通过删除required = true来解决它。现在它有效。 感谢tipps到目前为止!