Sunday, October 17, 2010

Maven: Add Spring dependencies and Spring MVC your project

To enable Spring and Spring MVC modules in your maven project, follow the steps below

#1: Add Spring related dependencies. The dependencies below will get all the rest of the transitive dependencies.


  
    org.springframework
    spring-context
    ${org.springframework.version}

    
      
      
        commons-logging
        commons-logging
      
    

    
      
      org.springframework
      spring-webmvc
      ${org.springframework.version}
    


The org.springframework.version property is set to 3.0.3.RELEASE:

3.0.3.RELEASE

  

#2. Setup Dispatcher Servlet in your web.xml:

The Spring MVC requires setting the dispatcher servlet in web.xml. Below we are loading the /WEB-INF/sprin/app-config.xml (the main Spring application configuration file for the project) while instantiating the Dispatcher Servlet at application start up time i.e. when the server is starting up.

NOTE: You can name your application config with any name of your liking. Just make sure it is correctly referenced in Dispatcher Servlet setting in web.xml.


    springmvcdemo
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation/WEB-INF/spring/app-config.xml
      
    1
  

  
    springmvcdemo
    /app/*
  


#3. The context config location points to the spring application config file for the application (app-config.xml). There are 2 things of note here:

a. The com.mayabansi.webappdemo will be scanned for classes annotated with @Controller, @Resources. @Component, @Service etc.

b. The mvc-config.xml, that holds the MVC configuration settings is imported.


    

    
    

#4. The MVC config file has Spring MVC related configuration settings:


    

    
    

    
    
        
        
    

No comments:

Post a Comment

Your comments are welcome. It will help me improve my communication and content...plus it will encourage me! :-)