Thursday, June 6, 2013

Spring MVC setup using Java Config

Add following config in the web.xml. The following config registers Spring listener. The listener knows that the configuration is based on Java Config by context param "contextClass". The context param "contextConfigLocation" tells the starting configuration class.
 <context-param>
  <param-name>contextClass</param-name>
  <param-value>
   org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
 </context-param>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>com.project.config.ApplicationConfiguration</param-value>
 </context-param>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
Sample configuration class. Note the "@Configuration" which tells that this class is a configuration class. The below configuration will scan package com.project and look for @Configuration, @Service, @Repository, @Component annotated class and create beans. Note that @Controller is excluded as I like to keep that separate in dispacther servlet context (explained later in the article). Also "WebConfiguration.class" is excluded as it will be used in the dispatcher servlet context.
 @Configuration
 @ComponentScan(basePackages = "com.project", excludeFilters = { @Filter(value = WebConfiguration.class, type = FilterType.ASSIGNABLE_TYPE),
   @Filter(value = Controller.class) })
 public class ApplicationConfiguration {
 }
Add following config in the web.xml. The following config registers a dispatcher servlet. The dispatcher servlet routes urls to the respective controller. In this example urls ending with ".do" will be handled by Spring MVC.

The servlet knows that the configuration is based on Java Config by init param "contextClass". The init param "contextConfigLocation" tells the starting configuration class.
 <servlet>
  <servlet-name>SpringDispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextClass</param-name>
   <param-value>
    org.springframework.web.context.support.AnnotationConfigWebApplicationContext
   </param-value>
  </init-param>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>com.project.server.config.WebConfiguration</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>SpringDispatcher</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
Sample configuration class. Note the "@Configuration" which tells that this class is a configuration class. The below configuration will scan package "com.project" and look for @Configuration, @Component annotated class and create beans. Note that @Service, @Repository are excluded as I like to keep that separate in application context (see above). Also "ApplicationConfiguration.class" is excluded as it will be used in the application context.
 @Configuration
 @ComponentScan(basePackages = "com.project", excludeFilters = { @Filter(value = Service.class), @Filter(value = Repository.class),
   @Filter(value = ApplicationConfiguration.class, type = FilterType.ASSIGNABLE_TYPE) })
 @EnableWebMvc
 public class WebConfiguration {
 }

No comments:

Post a Comment