Servlet3.0下基于注解的SpringMVC3.1配置-完全零配置文件
Spring3.1.x系列的一个新特性就是支持了Servlet3.0规范,基于注解的方式配置SpringMVC框架。官方文档描述如下: 3.1.10 Support for Servlet 3 code-based configuration of Servlet Co...
Spring3.1.x系列的一个新特性就是支持了Servlet3.0规范,基于注解的方式配置SpringMVC框架。官方文档描述如下:
3.1.10 Support for Servlet 3 code-based configuration of Servlet Container
The new WebApplicationInitializer builds atop Servlet 3.0's ServletContainerInitializer support to provide a programmatic alternative to the traditional web.xml.
See org.springframework.web.WebApplicationInitializer Javadoc
Diff from Spring's Greenhouse reference application demonstrating migration from web.xml to WebApplicationInitializer
既然如此,我们就来实现一下。根据WebApplicationInitializer 的注释说明,实现该接口即可。
/**
* 基于Servlet3.0的,相当于以前<b>web.xml</b>配置文件的配置类
*
* @author OneCoder
* @Blog http://www.coderli.com
* @date 2012-9-30 下午1:16:59
*/
public class DefaultWebApplicationInitializer implements
WebApplicationInitializer {
@Override
public void onStartup(ServletContext appContext)
throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(DefaultAppConfig.class);
appContext.addListener(new ContextLoaderListener(rootContext));
ServletRegistration.Dynamic dispatcher = appContext.addServlet(
"dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}这里说明一下,根据WebApplicationInitializer 类的注释说明(OneCoder建议大家多阅读代码注释(Javadoc),其实也就是API文档。),其实这里有两种初始化rootContext的方式。一种是利用XmlWebApplicationContext,即Spring的配置是通过传统的Xml的方式配置的。另一种就是这里用到的AnnotationConfigWebApplicationContext ,即Spring的配置也是通过注解的方式。这里,我们既然说了是完全零配置文件,那么我就采用第二种方式。利用新建的配置类,配置注解的方式,完成配置。
/**
* Spring3.1基于注解的配置类, 用于代替原来的<b>applicationContext.xml</b>配置文件
*
* @author lihzh
* @date 2012-10-12 下午4:23:13
*/
@Configuration
@ComponentScan(basePackages = "com.coderli.shurnim.*.biz")
public class DefaultAppConfig {
}可以看到,基本原有在配置文件里的配置,这里都提供的相应的注解与之对应。这里我们仅仅配置的包扫描的根路径,用于SpringMVC配置的扫描,以进一步达到零配置文件的效果。在业务包下,写一个MVC的controller类:
/**
* 用户操作Action类
*
* @author lihzh
* @date 2012-10-12 下午4:12:54
*/
@Controller
public class UserAction {
@RequestMapping("/user.do")
public void test(HttpServletResponse response) throws IOException {
response.getWriter().write("Hi, u guy.");
}
}启动Tomcat,浏览器访问地址: http://localhost:8080/onecoder-shurnim/user.do
看完题解,马上用 240 关实战检验掌握度
覆盖 GESP 1~8 级考纲心法、CSP-J 双栏阅读程序、错题动态变式推演与 90/120 分钟全真模考。
配套题解代码归档于 yummy-code 仓库,完整知识点索引见 GESP Wiki。
同专栏相关推荐
Java 利用ASM读取变量值(Field value)问题研究
最近在学习Spring源码的过程中,遇到了spring-asm工程的重新打包的问题,于是突然就想研究一下asm这个开源字节码操作工具。秉承我的一贯风格,想到啥就立马学啥。 对于开源产品,我的一贯风格就是通过其官方提供的源码版本管理地址(svn/git等),直接下载最新代...
SpringFramework3 源码下载和编译教程
Spring已经将源码从svn迁移到了git。所以,首先需要安装git环境。我使用的是Windows下的TortoiseGit,带图形界面,比较直观。 * SpringFramework的GitHub地址:https://github.com/SpringSource/...
Spring研究-容器初始化之FileSystemXmlApplicationContext构造函数
宅男Coder,没有其他爱好,闲暇之余抱着瞻仰的心态去阅读一下Spring的源码,期许能收获一支半解。要学习Spring的源码,第一步自然是下载和编译Spring的源码,这个我在之前的博文中已经发表过了。具体可参考: 《SpringFramework源码下载和编译教程》...
OneCoder
始于 2012持续记录 C++ GESP/CSP 信奥算法、Java 服务端架构与 Python 工程实战。官方唯一域名:coderli.com