7.3. Start-up Code — WebInitializer

Now we’ll get rid of the Web.xml file and create the WebInitializer class in its place:

  1. package ru.ibase.fbjavaex.config;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.ServletRegistration.Dynamic;
  5. import org.springframework.web.WebApplicationInitializer;
  6. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  7. import org.springframework.web.servlet.DispatcherServlet;
  8. public class WebInitializer implements WebApplicationInitializer {
  9. @Override
  10. public void onStartup(ServletContext servletContext) throws ServletException {
  11. AnnotationConfigWebApplicationContext ctx =
  12. new AnnotationConfigWebApplicationContext();
  13. ctx.register(WebAppConfig.class);
  14. ctx.setServletContext(servletContext);
  15. Dynamic servlet = servletContext.addServlet("dispatcher",
  16. new DispatcherServlet(ctx));
  17. servlet.addMapping("/");
  18. servlet.setLoadOnStartup(1);
  19. }
  20. }

All that is left to configure is IoC containers for injecting dependencies, a step we will return to later. We proceed next to generating classes for working with the database via Java Object-Oriented Querying (jOOQ).