本项目代码地址:demo-world (spring-boot-demo模块)
这里是spring-boot模块的第一期文章,主要来讲一下spring-boot是什么?怎么用?为什么要用?
1.What is SpringBoot?
官方文档开头说明了这个项目的目的:
Our primary goals are:
Provide a radically faster and widely accessible getting-started experience for all Spring development.
Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).
Absolutely no code generation and no requirement for XML configuration.
简而言之,spring-boot具有一下几个特点:
- 使用大量的默认配置代替手动配置,(如果有特别需要个别项也可以自定义配置)
- 使用.yml代替xml配置,增加了配置的可读性
- 内置tomcat,因此可以使用jar包部署,不必使用war包(也不需要额外的tomcat环境)
2.一个极简的SpringBoot项目:
接下来我将用三段代码(本例的全部代码)来呈现有个项目:
首先是maven的pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.swing</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后是一个接口:
@RestController
@RequestMapping("/first")
public class FirstController {
@RequestMapping("/")
String home() {
return "Hello World!";
}
}
最后是项目的启动入口:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
System.out.println("启动成功啦!");
}
}
OK ! 这就是用Spring Boot 搭建的一个web项目的全部了,炒鸡简单吧!甚至我们连tomcat都不需要了
spring是如何做到这一点的呢?答案在 @EnableAutoConfiguration注解中(此注解包含在@SpringBootApplication注解中)官方文档对它的解释是:
The second class-level annotation is
@EnableAutoConfiguration
. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Sincespring-boot-starter-web
added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
总的来说,springBoot会根据你的依赖自动揣摩你这大概是个什么类型的项目,然后使用一些常用的配置来配置此项目(例如需要使用 tomcat ,默认端口为8080等)
接下来讨论一下主方法,这是项目的运行入口,通过调用run委托给Spring Boot的SpringApplication类。run方法的args参数意在获取运行时传入的参数,例如使用 --spring.profiles.active 指定运行环境(开发,测试,部署)
3.更自由的配置springBoot
体验过上面的快捷配置,但仔细想想,是否有一种配置可以在简便的同时更加自由,确实有,那就是 使用application.yml
官网为我们给出给出了所有的默认的配置:链接 (当然,这里面不包括一些第三方的框架对spring-boot的支持,例如spring-boot-mybatis等)
扩展一点:上面提到过一个运行环境(dev,test,prod),项目在不同环境下运行需要不同的 application.yml 配置,我们通常使用如下配置文件名:
- applicaiton-dev.yml
- application-test.yml
- application-prod.yml
当需要指定某一个运行环境时候,只需要如下写法即可:
java -jar (要运行的jar包地址)--spring.profiles.active=dev