Spring Configuration and ServletInitizer

Starting from Spring 3.1, you can run the app without web.xml and dispatch-servlet.xml. Simply storing all your settings in Spring configuration and servlet initizer, it helps to simplify the web configuration with Spring.

Let’s create a Spring configuration file called AppConfiguration.java under src/main/java/com/itblogs/config/ as:

package com.itblogs.config;

import org.springframework.context.MessageSource;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.support.ResourceBundleMessageSource;

import org.springframework.util.StringUtils;

import org.springframework.web.servlet.LocaleResolver;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import org.springframework.web.servlet.view.InternalResourceViewResolver;

import org.springframework.web.servlet.view.JstlView;

/**

 * @author      Kelvin Hui

 * @see              it-blogs.com

 * @since            01-Jan-2016

 */

//@Configuration     – With this annotation, Spring knows AppConfiguration.class containing Spring configuration and bean definitions

//@EnableWebMvc            = <mvc:annotation-driven/>

//@ComponentScan     = <context:component-scan base-package=”com.itblogs” />

@Configuration

@EnableWebMvc

@ComponentScan(basePackages = “com.itblogs”)

public class AppConfiguration extends WebMvcConfigurerAdapter {

       //     <mvc:resources mapping=”/resources/**” location=”/resources/”/>

       @Override

       public void addResourceHandlers(ResourceHandlerRegistry registry) {

              registry.addResourceHandler(“/resources/**”).addResourceLocations(“/resources/”);

       }

       //     <bean id=”localeChangeInterceptor” class=”org.springframework.web.servlet.i18n.LocaleChangeInterceptor”>

       //            <property name=”paramName” value=”lang” />

       //     </bean>

       //     <bean id=”handlerMapping” class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”>

       //            <property name=”interceptors“>

       //                   <list>

       //                         <ref bean=”localeChangeInterceptor”/>

       //                   </list>

       //            </property>

       //     </bean>

       @Override

       public void addInterceptors(InterceptorRegistry registry) {

              LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();

              localeChangeInterceptor.setParamName(“lang”);

              registry.addInterceptor(localeChangeInterceptor);

       }

       //     <bean id=”localeResolver” class=”org.springframework.web.servlet.i18n.SessionLocaleResolver”>

       //            <property name=”defaultLocale” value=”en“/>

       //     </bean>

       @Bean

       public LocaleResolver localeResolver() {

              SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();

              sessionLocaleResolver.setDefaultLocale(StringUtils.parseLocaleString(“en”));

              return sessionLocaleResolver;

       }

       //     <bean id=”messageSource” class=”org.springframework.context.support.ResourceBundleMessageSource”>

       //            <property name=”basename” value=”messages/messages” />

       //            <property name=”defaultEncoding” value=”UTF-8″ />

       //     </bean>

       @Bean

       public MessageSource messageSource() {

              ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

              messageSource.setBasename(“messages/messages”);

              messageSource.setDefaultEncoding(“UTF-8”);

              return messageSource;

       }

       //     <bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>

    //        <property name=”prefix” value=”/WEB-INF/jsp/” />

    //        <property name=”suffix” value=”.jsp” />

       //     </bean>

       @Bean

       public InternalResourceViewResolver viewResolver() {

              InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

              viewResolver.setViewClass(JstlView.class);

              viewResolver.setPrefix(“/WEB-INF/jsp/”);

              viewResolver.setSuffix(“.jsp”);

              return viewResolver;

       }

}

 

Now, create the Initializer.java under the same folder:

package com.itblogs.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**

 * @author      Kelvin Hui

 * @see              it-blogs.com

 * @since            01-Jan-2016

 */

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override

    protected Class<?>[] getRootConfigClasses() {

        return new Class[] { AppConfiguration.class };

    }

    @Override

    protected Class<?>[] getServletConfigClasses() {

        return null;

    }

    // = <mvc:default-servlet-handler/>

    @Override

    protected String[] getServletMappings() {

        return new String[] { “/” };

    }

}

 

Yeap! Spring configuration is done… you can find the explanation of each settings above. However, if you still want to use web.xml and dispatch-servlet.xml, please see “Updating Web.xml and Dispatch-servlet.xml”.

Please rate this