Reputation: 85
I have started migration a Spring MVC / Spring Web Tomcat App to Spring Boot. Currently I am migrating the xml configuration files to java configuration.
When I try to start my application via mvn spring-boot:run I get the following error:
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
The code snippet causing the error is
@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;
...
On researching the web I just found, that Autowiring the HttpServletRequest should always work fine...
Before migration to java based configuration the application worked fine as WAR file placed inside a tomcat.
Any hints what I am missing? If further information is required just tell me, I will provide relevant code.
KR Habib
Upvotes: 1
Views: 4406
Reputation: 109
For me we had "spring.main.web-application-type=none" mistakenly set in our spring properties file, it shouldn't have been set to none as that was making spring not get the javax.servlet.http.HttpServletRequest bean, resulting in a very similar error to the one you have posted.
Upvotes: 0
Reputation: 85
I solved it by removing the required=true. Now it works. Thanks for the tipps so far!
Upvotes: 0
Reputation: 2477
Since HttpServletRequest
is request scoped bean, you can only inject it into request object; example @Controller
classes.
So move @Autowired(required = true) private HttpServletRequest request;
to controller class and pass the reference to @Service
classes through setter.
Upvotes: 1