Mix web.xml and AbstractAnnotationConfigDispatcherServletInitializer in Spring

I have an application in Spring and using Java Configs to configure and initialize my application, so I don't have web.xml . This is what my web initializer looks like,

 public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); } @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{PublicApiConfig.class, MobileConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/*"}; } @Override protected Filter[] getServletFilters() { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); LoggingFilter loggingFilter = new LoggingFilter(); return new Filter[]{characterEncodingFilter, loggingFilter}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[0]; } } 

I need to implement tomcat session replication, and for the purpose I need the application to be distributable . With traditional web.xml, I could add the <distributable/> attribute and that is it. However, as I understand it, this cannot be done using Java Configs.

My question is is it possible to have mixed web.xml and java configurations, for example. have

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <distributable/> </web-app> 

and enable it in the WebInitializer .

+1
source share
2 answers

You can use TomcatEmbeddedServletContainerFactory and there

 @Override public void customize(Context context){ context.setDistributable(true); } 

You will find a complete code example in this thread spring-boot-application-with-embedded-tomcat-session-clustering

Edit: I do not use Spring Boot in this case, and TomcatEmbeddedServletContainerFactory is not available

The javadoc WebApplicationInitializer says that it can be used with web.xml:

The use of WEB-INF / web.xml and WebApplicationInitializer is not mutually exclusive; for example, web.xml can register one servlet, and WebApplicationInitializer can register another. The initializer can even change the registration performed in web.xml using methods such as ServletContext # getServletRegistration (String).

+1
source

According to the Servlet 3.0 specification, it is possible to mix web.xml with the registration of the Programmatic servlet if the attribute web-app version> = 3.0 and metadata-complete is false (default). It should work with your current configuration.

+1
source

All Articles