0

I've a spring boot project (executable jar with embedded jetty9).

I should register 2 different DispatcherServlet. I done in this way:

@Bean
public ServletRegistrationBean dispatcherServlet() {
    DispatcherServlet servlet = new DispatcherServlet();
    ServletRegistrationBean sr = new ServletRegistrationBean(servlet, "/graniteamf/*");
    return sr;
}

@Bean
    @Order(2)
    public ServletRegistrationBean customDispatcherServlet() {
        DispatcherServlet servlet = new DispatcherServlet();
        ServletRegistrationBean sr = new ServletRegistrationBean(servlet, "/client/*");
        return sr;
    }

Unfortunally when I try to start the application I've this exception:

17/02/2015 18:24:04  INFO ApacheAsyncTransport:54 - Starting Apache HttpAsyncClient transport...
17/02/2015 18:24:04  INFO ApacheAsyncTransport:54 - Apache HttpAsyncClient transport started.
17/02/2015 18:24:04 DEBUG Application:173 - Inizializzazione identity
17/02/2015 18:24:04 ERROR DispatcherServlet:497 - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:612)
    at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:395)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:871)
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedWebAppContext$JettyEmbeddedServletHandler.deferredInitialize(JettyEmbeddedWebAppContext.java:46)
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedWebAppContext.deferredInitialize(JettyEmbeddedWebAppContext.java:36)
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.handleDeferredInitialize(JettyEmbeddedServletContainer.java:162)
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.start(JettyEmbeddedServletContainer.java:109)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:287)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at it.pianetatecno.gsmgateway.Application.main(Application.java:61)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    ... 30 more
17/02/2015 18:24:05 ERROR SpringApplication:339 - Application startup failed
org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Jetty servlet container
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.start(JettyEmbeddedServletContainer.java:119)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:287)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at it.pianetatecno.gsmgateway.Application.main(Application.java:61)

I've two questions:

  1. xml file for dispatcherServlet is mandatory?
  2. in which folder Spring expect to find this file? I tried to put it into resources/WEB-INF but it doesn't work.

Edited question

This is my main mathod in Spring Boot:

@SpringBootApplication

public class Application { private static Logger log = Logger.getLogger(Application.class);

private static ApplicationContext context;

public static void main(String[] args) {        
    SpringApplication app = new SpringApplication(Application.class);
    app.setShowBanner(false);
    context = app.run(args);
    app.setShowBanner(false);
}

Thanks

3
  • 1
    Why do you need multiple servlets? The whole point of Front Controller is that you usually only need one. Feb 17, 2015 at 18:19
  • Let's say I use only one DispatcherServlet. Also in this case I've the exception. Spring try to load the xml but I don't find the best practice to configure DispatcherServlet with Spring boot. Thanks
    – drenda
    Feb 17, 2015 at 18:22
  • 2
    You just don't. At all. Include starter-web and Spring Boot does everything automatically. Feb 17, 2015 at 18:32

1 Answer 1

1

You don't need XML, but you do need to set the ApplicationContext on the servlet. Spring will do that for you if it is a @Bean (as it is in the DispatcherServletAutoConfiguration in Spring Boot), or I guess you can do it manually.

4
  • I added my main method on the original question. Is not so clear how make available this context to DispatcherServlet.
    – drenda
    Feb 17, 2015 at 17:58
  • You can inject the application context as a method parameter in any @Bean method, or you can @Autowired it as a field or setter. This is standard Spring stuff (not Boot). Or you could do as I suggested and make the servlets into beans.
    – Dave Syer
    Feb 17, 2015 at 18:39
  • 1
    @Dave Is this always going to be the same ApplicationContext instance? Does boot have separate (Web)ApplicationContext instances, one for DispatcherServlet, one for ContextLoaderListener, one for etc.? Feb 18, 2015 at 0:49
  • Spring Boot normally works with a single application context, but there's nothing stopping you from creating others if you ned them.
    – Dave Syer
    Feb 18, 2015 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.