本篇记录使用纯java代码搭建SpringMVC工程的实践,只是一个demo。再开始之前先热身下,给出SpringMVC调用流程图,讲解的是一个http request请求到达SpringMVC框架后的过程,如下:

从servlet 3.0开始,实现javax.servlet.ServletContainerInitializer接口的类将在容器启动的时候执行onStartup方法。SpringMVC的”零配置”就是基于这个特性。所以对于servlet 3.0 以下的容器还是老老实实在web.xml中进行配置。 下面让我们一步步搭建SpringMVC,先上图:

上图中展示了启动关键类的初始化过程,首先容器启动,初始化并执行实现ServletContainerInitalzer接口的Spring类,该类初始化实现WebApplicationInitializer接口的PlayWebAppInitializer类(由于这里继承了AbstractAnnotationConfigDispatcherServletInitializer抽象类)。

在讲解PlayWebAppInitializer初始化了两个bean容器,一种是与DispatcherServlet相关联的MVC框架直接关系的bean,如:Controller,Service,Repository等等。再说另外一种bean容器前先回忆下,还记得ContextLoaderListener吧,做SSH框架整合时在web.xml中要配置这个类,所以说这个容器是配置更通用一些的bean。 那么问题来了:为什么要有两个共存呢?----答:我猜想是方便分离,如果不想使用SpringMVC而实用其他MVC框架的可以把DispatcherServlet相关的那个bean容器拿掉。(纯属意淫,欢迎拍砖)。

这里WebConfig用来负责DispacherServlet相关bean的配置,RootConfig用来负责ContextLoaderListener相关bean的配置。

下面截取一段《Spring in Action 4th》中关于两个ApplicationContext的原话,在135页:

A TALE OF TWO APPLICATION CONTEXTS

When DispatcherServlet starts up, it creates a Spring application context and starts loading it with beans declared in the configuration files or classes that it’s given. With the getServletConfigClasses() method in listing 5.1, you’ve asked that Dispatcher- Servlet load its application context with beans defined in the WebConfig configura- tion class (using Java configuration). But in Spring web applications, there’s often another application context. This other application context is created by ContextLoaderListener. Whereas DispatcherServlet is expected to load beans containing web components such as controllers, view resolvers, and handler mappings, ContextLoaderListener is expected to load the other beans in your application. These beans are typically the middle-tier and data-tier components that drive the back end of the application. 136 CHAPTER 5 Building Spring web applications Under the covers, AbstractAnnotationConfigDispatcherServletInitializer cre- ates both a DispatcherServlet and a ContextLoaderListener. The @Configuration classes returned from getServletConfigClasses() will define beans for Dispatcher- Servlet’s application context. Meanwhile, the @Configuration class’s returned get- RootConfigClasses() will be used to configure the application context created by ContextLoaderListener. In this case, your root configuration is defined in RootConfig, whereas Dispatcher- Servlet’s configuration is declared in WebConfig. You’ll see what those two configura- tion classes look like in a moment.

上实践的代码: com.bob.playspring.PlayWebAppInitializer

package com.bob.playspring;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
* 初始化DispatherServlet,代替在web.xml中到DispatherServlet配置,
* @author bob
*
*/
public class PlayWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { RootConfig.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
} /**
* identifies one or more paths that DispatcherServlet will be mapped to.<br>
* In this case, it’s mapped to /, indicating that it will be the application’s default servlet.<br>
* It will handle all requests coming into the application.
*/
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
} }

com.bob.playspring.WebConfig

package com.bob.playspring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver; /**
* 系统配置
* @author bob
*
*/
@EnableWebMvc
@Configuration
@ComponentScan("com.bob")
public class WebConfig extends WebMvcConfigurerAdapter { @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
} }

com.bob.playspring.RootConfig

package com.bob.playspring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
@ComponentScan(basePackages = { "com.bob" }, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig { }

关于Controller,Service,Repository的代码我就不贴了。

零配置文件搭建SpringMVC实践纪录的更多相关文章

  1. 零配置文件搭建SpringMvc

    零配置文件搭建SpringMvc SpringMvc 流程原理 (1)用户发送请求至前端控制器DispatcherServlet:(2) DispatcherServlet收到请求后,调用Handle ...

  2. 使用IDEA搭建一个 Spring + Spring MVC + Mybatis 的Web项目 ( 零配置文件 )

    前言: 除了mybatis 不是零配置,有些还是有xml的配置文件在里面的. 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration  ...

  3. 零配置简单搭建SpringMVC 项目

    SpringMVC是比较常用的JavaWeb框架,非常轻便强悍,能简化Web开发,大大提高开发效率,在各种Web程序中广泛应用.本文采用Java Config的方式搭建SpringMVC项目,并对Sp ...

  4. 脚手架快速搭建springMVC框架项目

    apid-framework脚手架快速搭建springMVC框架项目   rapid-framework介绍:   一个类似ruby on rails的java web快速开发脚手架,本着不重复发明轮 ...

  5. Maven搭建SpringMVC+Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  6. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  7. idea使用maven搭建springmvc

    最近学着搭建springmvc,写此博客记录一下 idea版本:2016.3.1maven: apache-maven-3.3.9tomcat:apache-tomcat-8.5.8 1.New Pr ...

  8. 1、搭建springMVC开发环境以及HelloWorld测试

    一.下载spring-framework,采用简单的方式: http://repo.springsource.org/libs-release-local/org/springframework/sp ...

  9. Maven搭建SpringMVC+Hibernate项目详解

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

随机推荐

  1. 18.有一个网页地址, 比如PHP开发资源网主页: http://www.phpres.com/index.html,如何得到它的内容?

    方法1(对于PHP5及更高版本): $readcontents = fopen("http://www.phpres.com/index.html", "rb" ...

  2. github Android-Universal-Image-Loader

    目存在于Github上面https://github.com/nostra13/Android-Universal-Image-Loader,我们可以先看看这个开源库存在哪些特征 多线程下载图片,图片 ...

  3. iOS 开发:利用第三方插件来安装CoCoapods

    引言:通过上一篇博客我们知道了怎么样去通过终端来安装CoCoapods,这一篇我们着重与用第三方插件来安装CoCoapods: 1. 首先在提下链接下载插件 https://github.com/ka ...

  4. WPF中Popup的几个问题

    要用popup控件来解决一些问题.就此带来了一批问题. 问题一. 在popup外任意位置点击时要能关闭popup,这个本来简单,只要加上StaysOpen=false就可以了.但我的popup中有个O ...

  5. 日常开发使用GIT命令

    git是一种分布式的版本管理工具,现在我总结下日常使用的git命令 1)检出 git clone 地址 --检出代码 #这里的地址是代码池的地址,如Github或bitbucket 2)增加文件 gi ...

  6. yii2输出sql语句

    yii2如何输出具体的查询的sql语句: $query = User::find() ->where(['id'=>[1,2,3,4]) ->select(['username']) ...

  7. Javascript之confirm的用法

    confirm函数 confirm函数用于提供确认功能,它首先显示给定的message参数所包含的信息,并提供两个可选择的回答“ok”和“cancel”,然后等待用户选择其中的一个.如果用户选择“ok ...

  8. YbSoftwareFactory 代码生成插件【十六】:Web 下灵活、强大的审批流程实现(含流程控制组件、流程设计器和表单设计器)

    程序=数据结构+算法,而企业级的软件=数据+流程,流程往往千差万别,客户自身有时都搞不清楚,随时变化的情况更是家常便饭,抛开功能等不谈,需求变化很大程度上就是流程的变化,流程的变化会给开发工作造成很大 ...

  9. openWrt 安装与实践

    1. 先装一个编译用的环境, ubuntu 14 2. 在ubuntu里面安装svn, g++, libncurses5-dev git libssl-dev gawk, svn因为openwrt社区 ...

  10. CSU 1660 K-Cycle(dfs判断无向图中是否存在长度为K的环)

    题意:给你一个无向图,判断是否存在长度为K的环. 思路:dfs遍历以每一个点为起点是否存在长度为k的环.dfs(now,last,step)中的now表示当前点,last表示上一个访问的 点,step ...