I recently wanted to take a big step and move everything to Java based configuration. Worked perfect until now. There is just one issue.
There is this WebApplicationInitializer Interface from Spring, which was actually the first web.xml alternative AFAIK:
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
Better, there is this wonderfull AbstractAnnotationConfigDispatcherServletInitializer Class, which even more perfect to use if your configuration files (Dispatcher etc.) are all Java based:
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MyWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
And also an XML version of that, if your dispatcher is still stuck in xml:
public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext cxt = new XmlWebApplicationContext();
cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
return cxt;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Now my question is: I have mainly java based config files, so i am using the 2. case with AbstractAnnotationConfigDispatcherServletInitializer. But i have one XML file (for Spring Security), which i would really like to register, without changing my abstract class. For this case is the interface WebApplicationInitializer (1. case) is the only current solution for this (or can be a solution)? Or is there another way to achieve this without using the 1. case or without creating 2 WebApplicationInitializer Classes (1 with AbstractAnnotationConfigDispatcherServletInitializer and 1 with AbstractDispatcherServletInitializer)?