Spring boot Consider defining a bean named 'servletContext' in your configuration

3.1k views Asked by At

I have a project with several modules , and it's tomcat project.I need to convert to spring boot project.

Here is a xml file with follow bean : I have used @ImportResource("classpath:xxx.xml").

Here throws exception:


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in xxxService required a bean named 'servletContext' that could not be found.

Action:

Consider defining a bean named 'servletContext' in your configuration.

I have no idea about this problems almost three days. Please help me if you have any experiences about this, thanks a lot !

2

There are 2 answers

0
StasKolodyuk On

Just implement ServletContextAware interface instead of directly autowiring ServletContext

@Service
public class DummyService implements ServletContextAware {

    ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}