本文章会重点描述springboot第一个程序helloworld、springboot利用外置tomcat启动
一、Spring Boot简介
对于初学springmvc的人来说,想要入手就开发需要拷贝一连串的dependency而不知道这个是干嘛,不知道是不是少了依赖。像我刚接触springmvc的时候到处百度教程而发现各有不同,于是复制了一个又一个代码却不能自己设置,根本原因是不了解各个依赖的包。
Spring-Boot 正是为了解决繁复的代码配置而产生的。Spring-Boot 也是基于java-base 开发的代码,及不用xml文件配置,所有代码都由java来完成。还可以加入Groovy的动态语言执行。
总结:
Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。
简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题
Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。
二、预备知识
1.Maven的pom.xml文件结构之基本配置parent和继承结构
1.1 Maven项目的继承
Maven项目之间不仅存在多模块的聚合关系,而且Maven项目之间还可以存在相互继承的关系。
Maven项目之间的继承关系通过<parent>表示,在子Maven项目的POM中配置示例如下:
<parent> <groupId>org.springframework.boot</groupId> <!-- 自动包含以下信息: --> <!-- 1.使用Java6编译级别 --> <!-- 2.使UTF-8编码 --> <!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). --> <!-- 4.智能资源过滤 --> <!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). --> <artifactId>spring-boot-starter-parent</artifactId> <!-- spring boot 1.x最后稳定版本 --> <version>1.4.0.RELEASE</version> <!-- 表示父模块pom的相对路径,这里没有值 --> <relativePath /> </parent>
说明:给出被继承的父项目的具体信息。
其中的relativePath给出父项目相对于子项目的路径,这样在构件子项目时首先从该相对路径查找父项目,如果没有才会从本地库或进而远程库中查找父项目。
(在这里是没有设置这个relativePath 所以会直接去查找父项目)
其中的relativePath给出父项目相对于子项目的路径,这样在构件子项目时首先从该相对路径查找父项目,如果没有才会从本地库或进而远程库中查找父项目。
(在这里是没有设置这个relativePath 所以会直接去查找父项目)
三、用Spring Boot新建web项目
3.1 新建一个maven工程(注意,不要勾选create from archytype)
使用
① 新建一个maven项目。
② pom中parent设为 spring-boot-starter-parent 。建议使用最新的 RELEASE 版本。否则可能需要设置 <repositories/> 和<pluginRepositories/> 。
③ 添加应用需要的starter模块,作为示例,我们仅添加web starter模块。
这里需要解释下starter模块,简单的说,就是一系列的依赖包组合。例如web starter模块,就是包含了Spring Boot预定义的一些Web开发的常用依赖:
① 新建一个maven项目。
② pom中parent设为 spring-boot-starter-parent 。建议使用最新的 RELEASE 版本。否则可能需要设置 <repositories/> 和<pluginRepositories/> 。
③ 添加应用需要的starter模块,作为示例,我们仅添加web starter模块。
这里需要解释下starter模块,简单的说,就是一系列的依赖包组合。例如web starter模块,就是包含了Spring Boot预定义的一些Web开发的常用依赖:
项目结构如下
3.2 在pom.xml 中添加继承关系
<!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<!-- 自动包含以下信息: -->
<!-- 1.使用Java6编译级别 -->
<!-- 2.使UTF-8编码 -->
<!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). -->
<!-- 4.智能资源过滤 -->
<!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). -->
<artifactId>spring-boot-starter-parent</artifactId>
<!-- spring boot 1.x最后稳定版本 -->
<version>1.4.0.RELEASE</version>
<!-- 表示父模块pom的相对路径,这里没有值 -->
<relativePath />
</parent>
3.3 在当前项目路径下shift+右键 cmd命令输入mvn package 会将本地没有的jar自动下载,然后你可以在自己本地看到以下jar包
3.4 然后添加web依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
现在再次查看一下依赖树mvn dependency:tree
可以看出,spring-boot-starter-web 包含了很多内容,spring-webmvc、spring-web、jackson、validation、tomcat、starter。
以下是完整配置
<!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<!-- 自动包含以下信息: -->
<!-- 1.使用Java6编译级别 -->
<!-- 2.使UTF-8编码 -->
<!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). -->
<!-- 4.智能资源过滤 -->
<!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). -->
<artifactId>spring-boot-starter-parent</artifactId>
<!-- spring boot 1.x最后稳定版本 -->
<version>1.4.0.RELEASE</version>
<!-- 表示父模块pom的相对路径,这里没有值 -->
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 只在test测试里面运行 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-hello-world</finalName>
<plugins>
<!-- jdk编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
demo如下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
import org.springframework.web.bind.annotation.RestController;
@Controller
@EnableAutoConfiguration
public class Example {
@RequestMapping(value ="/home", method = RequestMethod.GET)
@ResponseBody
public String home(){
return "你好,Spring Boot";
}
public static void main(String[] args){
SpringApplication.run(Example.class, args);
}
}
运行结果
出现这个,等级为INFO即可
tomcat篇
在spring-boot中是内置tomcat 打包会自动变成jar,idea的运行要的是war这就要求我们说明打包方式
在pom.xml中添加
<packaging>war</packaging>
这个
Application类要继承
SpringBootServletInitializer
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
在pom.xml中添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
就能访问了
完整代码如下:http://download.csdn.net/download/sicily_winner/10185783