title: How can implement AbstractAnnotationConfigDispatcherServletInitializer for java config on my spring 3.2 and hibernate 4 integration project?
body: <p>On my application, I am using spring 3.2, jsf 2.1 and hibernate 4 integration. I want to replace configuration xml files to java config. For that I am focusing on AbstractAnnotationConfigDispatcherServletInitializer and WebMvcConfigurerAdapter section. </p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
  &lt;display-name /&gt;
  &lt;context-param&gt;
     &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
     &lt;param-value&gt;
     /WEB-INF/applicationContext.xml
     &lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.request.RequestContextListener
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;filter&gt;
      &lt;description&gt;generated-persistence-filter&lt;/description&gt;
      &lt;filter-name&gt;MySQLFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;
          org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
      &lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;entityManagerFactoryBeanName&lt;/param-name&gt;
          &lt;param-value&gt;MySQL&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;MySQLFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.primefaces.webapp.filter.FileUploadFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;   
  &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
      &lt;param-value&gt;server&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;com.sun.faces.expressionFactory&lt;/param-name&gt;
      &lt;param-value&gt;com.sun.el.ExpressionFactoryImpl&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;error-page&gt;
      &lt;exception-type&gt;javax.faces.application.ViewExpiredException&lt;/exception-type&gt;
      &lt;location&gt;/jsf/login.xhtml&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>how can implement context-param , welcome-file-list  and  error-page on javaconfig class.</p>
  
  <p>My javaconfig class is,</p>
  
  <pre><code>public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
  @Override
  protected Class&lt;?&gt;[] getRootConfigClasses() {
      return null;
  }
  
  @Override
  protected Class&lt;?&gt;[] getServletConfigClasses() {
      return new Class&lt;?&gt;[] { WebConfig.class };
  }
  
  @Override
  protected String[] getServletMappings() {
      return new String[] { "/", "/*", "*.faces", "*.xhtml" };
  }
  
  @Override  // for representing Faces Serelet context, is it correct?
  public void onStartup(ServletContext servletContext)
          throws ServletException {
      servletContext.addServlet("Faces Servlet", "javax.faces.webapp.FacesServlet");
  
  
      super.onStartup(servletContext);
  }
  
  @Override
  protected Filter[] getServletFilters() {
  
      CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
      characterEncodingFilter.setEncoding("UTF-8");
  
      OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter();
      openEntityManagerInViewFilter.setPersistenceUnitName("MySQL");
      return new Filter[] { characterEncodingFilter, openEntityManagerInViewFilter, new org.primefaces.webapp.filter.FileUploadFilter()};
  }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @Import(DatabaseConfig.class)
  @ComponentScan(basePackages = { "com.shiv.ShivCDB.web.jsf", "com.shiv.ShivCDB.service" })
  public class WebConfig extends WebMvcConfigurerAdapter {
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  }
  
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
  
      LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
      localeChangeInterceptor.setParamName("lang");
      registry.addInterceptor(localeChangeInterceptor);
  }
  
  @Bean
  public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
      SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
  
      Properties mappings = new Properties();
      mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
      mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
      mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
      b.setExceptionMappings(mappings);
      return b;
  }
  
  @Bean
  public LocaleResolver localeResolver() {
  
      CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
      cookieLocaleResolver.setDefaultLocale(StringUtils.parseLocaleString("en"));
      return cookieLocaleResolver;
  }
  
  @Bean
  public ViewResolver viewResolver() {
  
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setViewClass(JstlView.class);
      viewResolver.setPrefix("/WEB-INF/views");
      viewResolver.setSuffix(".jsp");
      return viewResolver;
  }
  
  @Bean
  public MessageSource messageSource() {
  
      ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
      messageSource.setBasenames("classpath:messages/messages", "classpath:messages/validation");
      // if true, the key of the message will be displayed if the key is not
      // found, instead of throwing a NoSuchMessageException
      messageSource.setUseCodeAsDefaultMessage(true);
      messageSource.setDefaultEncoding("UTF-8");
      // # -1 : never reload, 0 always reload
      messageSource.setCacheSeconds(0);
      return messageSource;
  }
  }
  </code></pre>