spring boot web 入门
① 新建一个maven项目。
② pom中parent设为 spring-boot-starter-parent 。建议使用最新的 RELEASE 版本。否则可能需要设置 <repositories/> 和<pluginRepositories/> 。
③ 添加应用需要的starter模块,作为示例,我们仅添加web starter模块。
这里需要解释下starter模块,简单的说,就是一系列的依赖包组合。例如web starter模块,就是包含了Spring Boot预定义的一些Web开发的常用依赖:
○ spring-web, spring-webmvc ================>>Spring WebMvc框架
○ tomcat-embed-* ================>> 内嵌Tomcat容器
○ jackson ================>> 处理json数据
○ spring-* ================>> Spring框架
○ spring-boot-autoconfigure ================>> Spring Boot提供的自动配置功能
换句话说,当你添加了相应的starter模块,就相当于添加了相应的所有必须的依赖包。
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.larry.spring</groupId>
<artifactId>larry-spring-demo4</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
package cn.larry.spring.controller; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@EnableAutoConfiguration
public class SampleController { @RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
} public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
@EnableAutoConfiguration 用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。
在上面这个例子中,@EnableAutoConfiguration 会判断出这是一个web应用,所以会创建相应的web环境。
SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤:
- 创建一个合适的ApplicationContext实例 (取决于classpath)。
- 注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。
- 刷新application context,加载所有单例beans。
- 激活所有CommandLineRunner beans。
默认,直接使用SpringApplication 的静态方法run()即可。但也可以创建实例,并自行配置需要的设置。
默认访问地址: http://localhost:8080/
按照之前的web项目习惯,你可能会问,怎么没有项目路径?
这就是Spring Boot的默认设置了,将项目路径直接设为根路径。
当然,我们也可以设置自己的项目路径 -- 在classpath下的 application.properties 或者 application.yaml 文件中设置即可。
# application.properties
# Server settings (ServerProperties)
server.port=8080
server.address=127.0.0.1
#server.sessionTimeout=30
server.contextPath=/aaa # Tomcat specifics
#server.tomcat.accessLogEnabled=false
server.tomcat.protocolHeader=x-forwarded-proto
server.tomcat.remoteIpHeader=x-forwarded-for
server.tomcat.basedir=
server.tomcat.backgroundProcessorDelay=30
上面, server.contextPath=/aaa 就是设置了项目路径。所以现在需要访问 http://localhost:8080/aaa/ 才行。
分析
OK,当目前为止,已经成功运行并访问了一个 SpringMVC 应用。简单的不能再简单了!
再来看一下启动时的信息:
第 9 行,启动SampleController。
第10行,查找active profile,无,设为default。
第11行,刷新上下文。
第12行,初始化tomcat,设置端口8080,设置访问方式为http。
第13行,启动tomcat服务。
第14行,启动Servlet引擎。
第15行,Spring内嵌的WebApplicationContext 初始化开始。
第16行,Spring内嵌的WebApplicationContext 初始化完成。
第17行,映射servlet,将 dispatcherServlet 映射到 [/] 。
第18行,映射filter,将 characterEncodingFilter 映射到 [/*] 。
第19行,映射filter,将 hiddenHttpMethodFilter 映射到 [/*] 。
第20行,映射filter,将 httpPutFormContentFilter 映射到 [/*] 。
第21行,映射filter,将 requestContextFilter 映射到 [/*] 。
第22行,查找 @ControllerAdvice。
第23行,映射路径 "{[/]}" 到 cn.larry.spring.controller.SampleController.home()。
第24行,映射路径 "{[/error]}" 到 org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)。
第25行,映射路径 "{[/error],produces=[text/html]}" 到 org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)。
第26行,略。 第27行,略。 第28行,略。 第29行,略。
第30行,tomcat启动完毕。
第31行,SampleController启动耗费的时间。
第32行,初始化 dispatcherServlet 。
第33行,dispatcherServlet 的初始化已启动。
第34行,dispatcherServlet 的初始化已完成。
第35行,收到shutdown关闭请求。
第36行,关闭AnnotationConfigEmbeddedWebApplicationContext。
第37行,略。
从上面的启动信息中可以明显看到SpringMVC的加载过程,特别需要注意的是这种默认方式下加载的几个 filter 。
spring boot web 入门的更多相关文章
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- Spring Boot Web 开发注解篇
本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot 从入门到实战汇总
之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...
- Spring Boot从入门到精通(二)配置GitHub并上传Maven项目
简单介绍一下GitHub,它是一个面向开源及私有软件项目的托管平台,因为只支持git作为唯一的版本库格式进行托管,故名GitHub. GitHub于2008年4月10日正式上线,除了Git代码仓库托管 ...
- Spring Boot从入门到精通(五)多数据源配置实现及源码分析
多数据源配置在项目软件中是比较常见的开发需求,Spring和Spring Boot中对此都有相应的解决方案可供大家参考.在Spring Boot中,如MyBatis.JdbcTemplate以及Jpa ...
- Spring Boot从入门到精通(六)集成Redis实现缓存机制
Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言 ...
随机推荐
- 【Linux】- Ubuntu安装redis,并开启远程访问
Ubuntu16.04安装Redis 开启Redis远程访问的步骤: 1.注释掉redis配置文件中的,bind 127.0.0.1 sudo vi /etc/redis/redis.conf #注释 ...
- 父类与子类的转换as,is
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#里面Console.Write()和Console.WriteLine()有什么区别?
Console.Write()和Console.WriteLine()都是System.Console提供的方法,两着主要用来将输出流由指定的输出装置(默认为屏幕)显示出来.两着间的差异在Consol ...
- .NET中SQL Server数据库连接方法
1. 使用本机上的SQL Server Express 实例上的用户实例. 用户实例的连接创建了一个新的SQL Server 实例.此连接只能是在本地SQL Server 2005实例上并 ...
- 【Python】从1<2<3的语法糖说起
python有一个很有意思的语法糖你可以直接写1<2<3. 这复合我们通常意义上的数学不等式,但对学过C等语言其实是有疑惑的. 我们知道不等式返回的其实是个Bool值,在C中是1,0因此C ...
- Go语言【第七篇】:Go函数
Go语言函数 函数是基本的代码块,用于执行某个任务.Go语言最少有个main()函数,可以通过函数来划分不同功能,逻辑上每个函数执行的是指定的任务.函数声明告诉了编译器函数的名称,返回类型和参数.Go ...
- BZOJ4299 Codechef FRBSUM(主席树)
感觉非常不可做,于是考虑有什么奇怪的性质. 先考虑怎么求子集和mex.将数从小到大排序,假设已经凑出了0~n的所有数,如果下一个数>n+1显然mex就是n+1了,否则若其为x则可以凑出1~n+x ...
- PHP导出excel,无乱码
php部分 header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes&qu ...
- 利用caffe的solverstate断点训练
你可以从系统 /tmp 文件夹获取,名字是什么 caffe.ubuntu.username.log.INFO.....之类 ====================================== ...
- Codeforces Round #342 (Div. 2) A
A. Guest From the Past time limit per test 1 second memory limit per test 256 megabytes input standa ...