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. 【原】Storm Local模式和生产环境中Topology运行配置

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  2. android studio 中获取sha1

    使用keytool 一.配置环境变量 由于要用到keytool工具,而keytool是jdk里的一个工具,首先将jdk/bin所在的目录加到环境变量的PATH中 看我的keytool所在位置 将所在路 ...

  3. vs2010 无法连接到asp.net development server

    http://blog.csdn.net/xqf309/article/details/7881257 今天打开之前的程序,按了F5进行调试,等了会弹出窗体来说:无法连接到asp.net develo ...

  4. 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较

    我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' ..   哪些不需要, grep与egrep的差异 ...

  5. 在Windows上,如何卸载RabbitMQ服务

    打开运行->CMD->sc delete RabbitMQ 如果报错..... 打开运行->regedit 找到RabbitMQ节点,删掉即可.(右侧看到的都是启动服务时,需要的配置 ...

  6. 教你区分LVDS屏线及屏接口定义

    现在碰到液晶屏大多是LVDS屏线,经常碰到什么单6,双6 单8双8.如何区分呢?我以前也不知道,后在网上收集学习后才弄明白方法1数带 “ -”的这种信号线一共有几对,有10对的减2对就是双8,有8对的 ...

  7. codeforces 653A Bear and Three Balls

    A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. [iOS 多线程 & 网络 - 2.0] - 发送接收 服务器信息

    A.搭建java服务器 使用eclipse.tomcat和struts2框架搭建一个简单的服务器 1.准备好合适版本的JDK.eclipse EE.tomcat.struts2 框架包 2.配置JDK ...

  9. Linux下的内核测试工具——perf使用简介

    Perf是Linux kernel自带的系统性能优化工具.Perf的优势在于与Linux Kernel的紧密结合,它可以最先应用到加入Kernel的new feature.pef可以用于查看热点函数, ...

  10. scp命令获取远程文件

    一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的,可能会稍微影响 ...