The release of Spring 2.5 reduce the burden of XML by introduction annotation based configuration, but you still needed to bootstrap Spring in XML. However in Servlet 3 and Spring 3.1 we can now drop XML completely and have 100% code based configuration. All thanks to the Servlet 3 specification.

So lets see today how you can create Spring MVC application XML Free.

Step-by-Step Guide

Step 1) Create a Enterprise Java project in Eclipse and include Spring v3.1+ jars.

Step 2) Create a class which implements spring WebApplicationInitializer and override onStartup() method.
Inside onStartup() method, instantiate AnnotationConfigWebApplicationContext class and set ServletContext. We also need to set a base package name of your configuration files.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package org.techzoo.springmvc.bootstrap;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
 
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
 
public class MyWebAppInitializer implements WebApplicationInitializer {
 
    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        final AnnotationConfigWebApplicationContext root = newAnnotationConfigWebApplicationContext();
        root.setServletContext(servletContext);
        root.scan("org.techzoo.springmvc.bootstrap");
        root.refresh();
 
        final Dynamic servlet = servletContext.addServlet("spring", newDispatcherServlet(root));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/*");
    }
 
}

Step 3) Next step is to configure Spring MVC. I do that in following class.
@EnableWebMvc annotation used together with @Configuration enables default Spring MVC configuration, equivalent to . With @ComponentScan annotation we make sure our @Controller will be added to the application context. The configuration class also defines one @Bean: our default view resolver.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package org.techzoo.springmvc.bootstrap;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
 
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.techzoo.springmvc.controller")
public class ControllerConfiguration {
    
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
 
}

Step 4) Now create a simple Controller to see whether its working fine or Not.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package org.techzoo.springmvc.controller;
 
import java.io.IOException;
import java.io.Writer;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class MyController {
 
    @RequestMapping(value = "/")
    public void home(@RequestBody final String body, final Writer writer)
    throws IOException
    {
        writer.append("<h2>Welcome, XML Free Spring MVC!</h2>");
    }
}

Output

I really like this configuration changes in Spring MVC. It is easy to bootstrap the application with no xml files in place. Of course, this is not everything that Spring MVC 3 brings to the developers. With all the configuration in code it is so easy to refactor and navigate. No more back and forth with XML For more feature of spring mvc, please keep visiting TechZoo.

Happy Coding 

reference from:http://www.techzoo.org/spring-framework/step-by-step-xml-free-spring-mvc-3-configuration.html

Step-by-Step XML Free Spring MVC 3 Configuration--reference的更多相关文章

  1. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  2. Spring MVC 学习笔记 spring mvc Schema-based configuration

    Spring mvc 目前支持5个tag,分别是 mvc:annotation-driven,mvc:interceptors,mvc:view-controller, mvc:resources和m ...

  3. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  4. 0077 web.xml中配置Spring MVC时,Servlet-name上报Servlet should have a mapping的错误

    这次是手工建立的web工程目录,在配置webapp/WEB-INF/web.xml的Spring MVC的DispatcherServlet时,在servlet-name上报错:Servlet sho ...

  5. [Spring MVC] - JSON

    Spring MVC中使用JSON,先必需引用两个包:jackson-core-asl-1.9.13.jar.jackson-mapper-asl-1.9.13.jar 因为需要使用到jquery测试 ...

  6. spring mvc基础配置

    web.xml 配置: <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> ...

  7. spring MVC学习笔记

    为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平.你的一个决定会影响团队未来的几年.要考虑方面太多: 1.简单易用,以提高开发效率.使小部分的精力在框架上,大部 ...

  8. Swagger+Spring mvc生成Restful接口文档

    简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...

  9. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

随机推荐

  1. Eclipse编辑java文件报Unhandled event loop exception错误的解决办法

    原因:电脑中安装了杀毒软件,卸掉或者关掉就可以了.我的是直接退出,错误就不产生了.

  2. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  3. HDU5727 Necklace

    http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:n个珠子,每个珠子有阴阳两种属性,且阴的一定和阳的紧邻,排成一个环:m行,每行两个数,表示阳性x珠子 ...

  4. Tkinter教程之Canvas篇(3)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811922 ''Tkinter教程之Canvas篇(3)''''''16.移动item'''# ...

  5. 取消本地SVN文件夹与服务器关联

    问题:之前建了一个SVN代码库,同步了代码上去,但中途发现建库时的规则搞错了,就把服务器上的库给删了重建,然后改变本地代码的svn服务器关联地址,但使用Relocate一直报错. 错误有两种情况:1. ...

  6. virt viewer Usbredir USB重定向

    编译virt viewer之前执行的configure命令,是没有使能usb-redir相关的功能,virt viewer是否支持usbredir是完全依赖于spice-gtk的. virt view ...

  7. Oracle 10G (Linux) 冷备恢复

    完整的数据库文件集包括:数据库启动参数文件,控制文件,数据文件,日志文件. 可以通过下面的方式来得到文件的位置和名称. 参数文件 $ORACLE_HOME/dbs/spfile[$ORACLE_SID ...

  8. Hibernate之Session对象的相关方法以及持久化对象的状态

    一.持久化对象的状态        站在持久化的角度, Hibernate 把对象分为 4种状态: 持久化状态,临时状态,游离状态,删除状态.Session 的特定方法能使对象从一个状态转换到另一个状 ...

  9. Mysql数据库插入的中文字段值显示问号的问题解决

    最近我使用myeclipse连接mysql数据库查询表中的数据,表中字段值为中文的字段显示问号,查了很多资料将解决方法总结如下: 步骤一:修改mysql数据库的配置文件my.ini或者my-defau ...

  10. POJ3126 Prime Path

    http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...