SpringMVC是比较常用的JavaWeb框架,非常轻便强悍,能简化Web开发,大大提高开发效率,在各种Web程序中广泛应用。本文采用Java Config的方式搭建SpringMVC项目,并对SpringMVC启动时加载顺序做简单的说明。

 1、SpringMVC启动流程图

2、SpringMVC项目启动流程介绍

SpringMVC 是Spring 框架的重要模块,借助于Spring 的容器技术,可以非常方面的搭建Web项目。

SpringMVC项目启动时要完成Spring 容器的初始化和SpringMVC配置的初始化。

2.1 Spring容器的初始化:

1、项目中需要配置ContextLoadListener监听器,它会监听项目的启动,在项目启动时调用容器初始化方法完成Spring容器的初始化

如果采用XML配置,通常需要在web.xml文件里面添加如下配置:

本文采用Java Config 实现,所以在配置中继承了AbstractAnnotationConfigDispatcherServletInitializer 这个抽象类,这个类的继承关系如下:

在父类AbstractContextLoaderInitializer中注册了ContextLoadListener监听器:

具体如下:

2、在需要在容器初始化时创建的类上面加上注解,就可以实现Bean的自动装配了。

@Controller @Service @Bean  @Conponent等注解都可以显示表明Bean需要自动装配

3、配置Bean初始化的范围

2.2 SpringMVC 的配置

1、配置SpringMVC需要添加DispatchServlet ,DispatcherServlet主要负责前端调用URL的分发,他在Web容器初始化的时候被注册。在2.1中,我们已经知道,本文配置中继承了DispatchServlet 的一个抽象类AbstractAnnotationConfigDispatcherServletInitializer ,此抽象类的父类在实例化的时候会注册一个DispatchServlet到容器中,方法名如下。

2、定义视图解析器

前端访问URL,DispatchServlet 会把URL 匹配到Controller中相应的@RequstMapper的方法上去,该方法处理完请求后返回需要的业务数据模型,然后会调用自己的视图解析器把数据渲染到前端页面中,渲染之后返回给浏览器。

视图解析器定义如下:

3、其实SpringMVC 项目启动时配置的东西还有很多,HandlerException 异常处理,数据校验等,这些Spring提供的抽象类WebMvcConfigurerAdapter中已经实现好了,我们在项目中直接继承就行了。

3、代码实现:

项目采用Maven管理:pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.beiyan</groupId>
<artifactId>demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<!-- Test -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

项目结构如下:

RootConfig类中配置了包扫描的范围:

package com.beiyan.demo.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(basePackages = { "com.beiyan.demo" })
public class RootConfig { }

WebConfig 中配置了视图解析器以及RequestMapper的扫描范围

package com.beiyan.demo.config;

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; @Configuration
@EnableWebMvc
@ComponentScan("com.beiyan.demo.controller")
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;
} /**
* 启用spring mvc 的注解
*/
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
} }

WebAppInitializer类配置了容器初始化时需要加载的配置类

package com.beiyan.demo.config;

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

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
} @Override
protected String[] getServletMappings() {
return new String[] { "/" };
} }

TestController中定义了测试用的URL:

package com.beiyan.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "test";
} }

项目的Web 文件如下:

启动项目后,浏览器访问http://localhost:8080/SpringServlet/test 可以成功访问如下:

至此,SpringMVC项目配置成功了。

写在后面的话:

本项目只是简单搭建了SpringMVC项目,采用了最简配置,实际项目中可能会对Servlet,Filter进行自定义,还会添加Mybatis /hibernate, SpringSecurity等功能。关于JPA,SpringSecurity,Themeleaf 的集成将会在下一篇关于SpringBoot 的项目搭建中集成。

本项目完整代码已上传开源中国Git仓库:https://git.oschina.net/beiyan/mvc-config

零配置简单搭建SpringMVC 项目的更多相关文章

  1. maven -- 学习笔记(四)实现在Eclipse用maven搭建springmvc项目(附构建步骤和详细实现代码)

    Learn from:http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html,感谢楼主的分享,才有下面的这篇学习小结 一.环境准 ...

  2. 创建一个可用的简单的SpringMVC项目,图文并茂

    转载麻烦注明下来源:http://www.cnblogs.com/silentdoer/articles/7134332.html,谢谢. 最近在自学SpringMVC,百度了很多资料都是比较老的,而 ...

  3. VSCode配置简单的vue项目

    VSCode配置简单的vue项目 https://www.cnblogs.com/wnxyz8023/p/9989447.html 由于最近要使用的项目框架为前后端分离的,采用的是vue.js+web ...

  4. 使用纯注解与配置类开发springMVC项目,去掉xml配置

    最近拜读了杨开振老师的书,深入浅出springBoot2.x,挖掘了很多以前被忽略的知识, 开发一年多,工作中一直用传统springmvc的开发,基本都还是用的传统的xml配置开发, 看到书里有提到, ...

  5. 项目搭建系列之一:使用Maven搭建SpringMVC项目

    约定电脑都安装了eclipse,且已配置好Maven以及eclipse插件. 1.Eclipse 2.maven 3.Eclipse 需要安装maven插件.url:maven - http://do ...

  6. 基于XML搭建SpringMVC项目

    *如果你需要将应用部署到不支持Servlet3.0容器中 或者 你只是对web.xml情有独钟,那我们只能按照传统的方式,通过web.xml来配置SpringMVC. *搭建SpringMVC需要在w ...

  7. 使用VSCode配置简单的vue项目

    由于最近要使用的项目框架为前后端分离的,采用的是vue.js+webAPI的形式进行开发的.因为之前我没有接触过vue.js,也只是通过视频文档做了一些简单的练习.今天技术主管说让大家熟悉下VSCod ...

  8. spirng: srping mvc配置(访问路径配置)搭建SpringMVC——最小化配置

    搭建SpringMVC——最小化配置 最开始接触网页的时候,是纯的html/css页面,那个时候还是用Dreamweaver来绘制页面. 随着网站开发的深入,开始学习servlet开发,记得最痛苦的就 ...

  9. 三、使用VSCode配置简单的vue项目

    由于最近要使用的项目框架为前后端分离的,采用的是vue.js+webAPI的形式进行开发的.因为之前我没有接触过vue.js,也只是通过视频文档做了一些简单的练习.今天技术主管说让大家熟悉下VSCod ...

随机推荐

  1. servlet中service() 和doGet() 、doPost() 学习笔记

    Sevlet接口定义如下: 与Sevlet接口相关的结构图: service() 方法是 Servlet 的核心.每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要 ...

  2. Linux从零单排(二):setuptools、pip、anaconda2的环境配置

    为了更方便的使用Python的类库,需要进行相应的配置 (一)setuptools的配置 1.setuptools的下载 命令行输入wget https://pypi.python.org/packa ...

  3. QStriingList

    #include <QCoreApplication> #include<QDebug> #include<QStringList> int main(int ar ...

  4. java常见的开发工具

    http://www.csdn.net/article/1970-01-01/2824723 http://zhidao.baidu.com/link?url=D8FdJxeFd-z-LV1OfZuZ ...

  5. BundleConfig.cs

    namespace Knockout.App_Start { public class BundleConfig { public static void RegisterBundles(Bundle ...

  6. python 面向对象(进阶篇)

    上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  7. Linux上设置memcached自启动

    #!/bin/sh # # memcached: MemCached Daemon # # chkconfig: - 90 25 # description: MemCached Daemon # # ...

  8. ca证书校验用户证书

    openssl verify -CAfile ca.cer server.crt 现在很多网站和服务都使用了HTTPS进行链路加密.防止信息在传输中间节点被窃听和篡改.HTTPS的启用都需要一个CA证 ...

  9. shell脚本一

    在一些复杂的Linux维护工作中,大量重复的输入和交互操作不但费时费力,容易出错.这时候就需要用到脚本. 编写脚本的好处:  批量的处理,自动化的完成维护,减轻管理员的负担. linux的shell脚 ...

  10. Spring配置AOP实现定义切入点和织入增强

    XML里的id=””记得全小写 经过AOP的配置后,可以切入日志功能.访问切入.事务管理.性能监测等功能. 首先实现这个织入增强需要的jar包,除了常用的 com.springsource.org.a ...