Springboot
Spring Boot 概述
Spring Boot 是什么
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。Spring Boot采用约定优于配置的方式,大量的减少了配置文件的使用。
一般我们把Spring Boot称为搭建程序的 脚手架 或者说是 便捷搭建基于Spring的工程脚手架 。主要的作用就是帮助开发人员快速构建Spring项目,并且尽可能的减少一切xml配置,做到开箱即用,快速上手,让开发人员关注业务而非配置。Spring Boot简化了基于Spring的应用开发,只需要“run” 就能创建一个独立的、生产级别的Spring应用。Spring Boot为Spring平台及第三方库提供了开箱即用的设置,这样我们就可以简单的开始。多数Spring Boot应用只需要很少的Spring配置。
Spring Boot 的特性
- 创建独立的Spring应用
- 直接嵌入 Tomcat、Jetty 或Undertow(无需部署 WAR 文件)
- 提供固定"启动器"依赖关系,以简化构建配置
- 尽可能自动配置Spring和第三方库
- 提供生产就绪型功能,如指标、运行状况检查和外部配置
- 绝对没有代码生成,也没有 XML 配置要求
为什么要用Spring Boot
Java(Spring)一直被人诟病的一点就是臃肿、麻烦,主要原因是以下两点:
- 复杂的配置
- 混乱的依赖管理
通过上小节Spring Boot的特性,我们可以看到,Spring Boot就是用来解决这些问题的。自动配置,可以节省我们大量的配置问题,Spring Boot为Spring和其他有需要的第三方依赖提供了默认配置,我们甚至可以不做任何配置就可以启动和运行项目。依赖关系,Spring Boot通过starter引入第三方依赖,其中的所有依赖关系都是经过测试和验证的,再加上Maven等依赖管理工具避免了依赖冲突。
Spring Boot 快速启动
启动一个提供Http请求的Restful的服务接口,请求地址为http://localhost:8080/hello 返回 Hello Spring Boot!
环境准备:
- JDK1.8.X
- Maven 3.6.X
- Eclipse 4.x/STS 4.x(Spring Tools Suite)/Idea
- Spring Boot 2.3.X
- Spring 5
创建项目工程
1.创建一个Spring Starter Project
2.填写相关信息
Name: HelloSpringBoot Java Version:8 Group: com.kayak Package: com.kayak.springboot.demo
3.选择版本,配置starter(启动器)
Spring Boot Version: 2.3.X Dependencies: Spring Web
完整的pom文件
<?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.3.RELEASE</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloSpringBoot</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
Spring Boot 项目通过main函数即可启动
package com.kayak.springboot.demo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class HelloSpringBootApplication {
public static void main(String[] args) { SpringApplication.run(HelloSpringBootApplication.class, args); }
}
编写 Controller
package com.kayak.springboot.demo.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello") public String hello() { return "Hello Spring Boot!"; }
}
使用启动类启动运行
2020-09-16 16:48:10.037 INFO 17152 --- [ main] c.k.s.demo.HelloSpringBootApplication : Starting HelloSpringBootApplication on DESKTOP-R2M87Q2 with PID 17152 (D:\work2\HelloSpringBoot\target\classes started by younge in D:\work2\HelloSpringBoot) 2020-09-16 16:48:10.040 INFO 17152 --- [ main] c.k.s.demo.HelloSpringBootApplication : No active profile set, falling back to default profiles: default 2020-09-16 16:48:12.897 INFO 17152 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-09-16 16:48:12.917 INFO 17152 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-09-16 16:48:12.918 INFO 17152 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37] 2020-09-16 16:48:13.058 INFO 17152 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-09-16 16:48:13.059 INFO 17152 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2907 ms 2020-09-16 16:48:13.413 INFO 17152 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-09-16 16:48:13.763 INFO 17152 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path 2020-09-16 16:48:13.781 INFO 17152 --- [ main] c.k.s.demo.HelloSpringBootApplication : Started HelloSpringBootApplication in 4.381 seconds (JVM running for 5.095) 2020-09-16 16:48:27.398 INFO 17152 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-09-16 16:48:27.398 INFO 17152 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-09-16 16:48:27.411 INFO 17152 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
1. banner:Spring Boot在启动的时候会有一个默认的启动图标,而且是可以自定义的 https://www.jianshu.com/p/bfbcabc4af1d
*启动类Main方法里面配置: SpringApplication::SetBanner(Banner)、SpringApplication::SetBannerMode(Mode)
*src\resources\banner.txt、banner.png/jpg:
#这个是MANIFEST.MF文件中的版本号 ${application.version} #这个是上面的的版本号前面加v后上括号 ${application.formatted-version} #这个是springboot的版本号 ${spring-boot.version} #这个是springboot的版本号 ${spring-boot.formatted-version} #枚举类AnsiColor这个可以替换显示的颜色 ${AnsiColor.***}
* application.properties: spring.main.show-banner=false
* 重写接口Banner实现
2. 默认profile为default
3. 默认容器为Tomcat
修改默认容器 1. 在starter-web的依赖里排除tomcat 2. 添加响应的容器starter依赖
java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
4. Tomcat默认端口为8080
properties
# 配置端口 server.port=9080
5. SpringMVC的项目路径是“”
properties
# 配置上下文路径 server.servlet.context-path=/yy
使用浏览器访问测试
创建可运行的Jar
我们通过创建一个完全独立的可执行 jar 文件来完成我们的示例,该文件可以在生产环境中运行。可执行 jar(有时称为"Fat jar")是包含编译类以及代码需要运行的所有 jar 依赖项的存档。
Java 不提供加载嵌套 jar 文件的标准方法(jar 文件本身包含在 jar 中)。如果您想要分发自包含的应用程序,这可能有问题。
为了解决这个问题,许多开发人员使用"uber"jar。Uber jar 将所有应用程序依赖项的所有类打包到单个存档中。此方法的问题在于,很难看到应用程序中的库。如果在多个 jar 中使用相同的文件名(但内容不同),也可能存在问题。
Spring Boot 采用不同的方法,让您直接嵌套 jar。
若要创建可执行 jar,我们需要将 spring-boot-maven-plugin添加到 我们的pom.xml 。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后我们就可以用maven打包了,打包成功之后我们会发现生成了两个文件,一个就是我们的“胖jar”,另一个会有“original”后缀,也就是原始的普通jar,是不能独立运行的。而“胖jar"就是可以独立运行的jar,如果我们把他解压开就会发现里面包含了所有需要依赖的jar文件。
BOOT-INF:主要是我们项目的东西
META-INF:工程描述文件
org:SpringBoot执行嵌套jar的方法
然后我们只需要使用 java -jar 就可以了。
可以将 Spring Boot 应用程序部署到各种云平台、容器映像(如 Docker)或虚拟/真实计算机
关于starter
starter,我们也称为启动器或者起步依赖,是Spring Boot中非常重要的一个概念,包括自动装配也都会用到它,Spring Boot提供了诸如web、data、test、sercuity等等应用级别的starter,另外还提供了下面两类:
Spring Boot production starters :spring-boot-starter-actuator 它提供生产就绪功能,帮助您监控和管理应用程序
Spring Boot technical starters :spring-boot-starter-jetty、spring-boot-starter-log4j2、spring-boot-starter-logging、spring-boot-starter-reactor-netty、spring-boot-starter-tomcat、spring-boot-starter-undertow
关于热部署
Spring Boot也是普通的java项目,所以JVM的热部署也应该是开箱即用的,自然也有一些更完善的实现。另外spring-boot-devtools也包含对应用快速重启的支持。打包的存档默认是不包含开发工具开发人员工具的,也就是说生产应用程序中自动禁用了该工具。
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional>
</dependency>
执行器:生产就绪功能
添加spring-boot-starter-actuator依赖,即可启用生产就绪功能 <dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <optional>true</optional>
</dependency>
actuator,我们称为执行器,提供EndPoints,我们称为断点,可让您监控应用程序并与应用程序进行交互。Spring Boot 包括许多内置端点,并允许您添加自己的端点。可以通过 HTTP 或 JMX 启用或禁用每个单独的终结点并公开(可远程访问),通常在HTTP上,映射的路径为为/actuator加上端点名,例如默认情况下HTTP公开了/actuator/health和/actuator/info(分别用于显示应用程序运行状况信息和显示任意应用程序信息)。
/actuator映射称为”发现页“,该页包含指向所有终结点的链接
Spring Boot的配置
Spring Boot 可以快速的实现SpringMVC的项目,免配置、快捷、高效!如果需要进行配置设置呢?
Spring Boot中的Java配置
Java配置主要靠一些Java类和注解来完成,常见的注解包括:
- @Configuration: 声明一个类作为配置类,代替xml文件
- @Bean: 声明一个发放,讲方法的返回值加入Bean容器(IOC容器),代替<bean>标签
- @ConfigurationProperties: 将配置文件中的健:值自动映射注入Java Bean
- @value: 属性注入
- @propertySource: 加载指定配置文件
- @ImportResource 导入Spring 配置文件
Spring Boot属性注入
Spring Boot的属性文件
Spring Boot默认的属性文件名称为:application.properties或者application.yml
属性的注入
@Value注入
直接使用 @Value (xxx) 注解就可以直接为某个属性注入全局配置文件中的值 @Value(“#{}”):表示 SpEl表达式。 @Value(“${xxx}”):表示从配置文件中取值,如果 "xxx" key 不存在,则启动时报错:Injection of autowired dependencies failed @Value(“${xxx:yyy}”):从配置文件中取值,如果 "xxx" key 不存在,则使用默认值 "yyy",否则启动会报错。
@ConfigurationProperties赋值
可以将全局配置文件中的键-值自动映射注入 Java Bean 中,Java bean 的属性必须提供 setter 方法才能注入值。 @ConfigurationProperties 可以标注在类、接口、枚举、注解、方法上 所在的类需要是 Spring 组件(@Component ) prefix 属性:用于将配置文件中指定的 key 下的所有属性与本类属性进行一一映射注入值,如果配置文件中不存在此 key,则不会为POJO注入值。如 prefix="user"、prefix="app.user" 。 ignoreInvalidFields 属性:是否忽略无效的字段,默认为 false 表示不忽略。配置的属性值类型错误,无法强转的字段就是无效字段,如果有无效字段且此属性为 false,则启动报错,无法正常启动应用,为 true 时则会忽略注入。 配置文件中没有为 java bean 配置相应的属性值,则不会注入值,java bean 属性仍然使用自己的默认值,不会影响程序启动。
/**
- 客户实体.
- 包含一些基本属性和一个账户属性。@ConfigurationProperties 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
- prefix = "customer" 表示将配置文件中key为customer的下面所有的属性与本类属性进行一一映射注入值,如果配置文件中不存在"customer"的key,则不会注入值,属性值仍然为默认值;
- ignoreInvalidFields = true 表示忽略配置文件中的无效字段 注解@Component 将本类标识为一个Spring
- 组件,因为只有是容器中的组件,容器才会为@ConfigurationProperties提供此注入功能
- @author younge
- @date 2020/09/18
- /
@Component
@ConfigurationProperties(prefix = "customer", ignoreInvalidFields = true)
public class Customer {
private Integer id;
private String name;
private Integer age;
private Account account;
...
}
- application.properties内容
customer.id:1
customer.name:Younge
customer.age:35
customer.account.accountNo:s1111
customer.account.amount:FF9999.99
@RestController
public class CustomerController {
// 必须使用@Resource、@Autowired 注入对象才能获取到它注入的属性值,直接 new 对象是没有注入的默认值的
@Autowired
private Customer customer;
@GetMapping(value = "/customer")
private String customer() {
return customer.toString();
}
}
@PropertySource导入配置文件
Spring Boot默认的全局配置文件“application.properties”或者“application.yml”,当应用比较大的时候,如果所有的内容都当在一个配置文件中,就会显得比较臃肿,同时也不太好理解和维护,此时可以将一个文件拆分为多个,使用 @PropertySource 注解加载指定的配置文件。
加载properties文件
@Component
@ConfigurationProperties(prefix = "customer", ignoreInvalidFields = true)
@PropertySource(value = {"classpath:customer.properties"})
public class Customer {
private Integer id;
private String name;
private Integer age;
private Account account;
...
}
配置文件内容:
customer.properties
customer:id:1
customer.name:Younge
customer.age:35
customer.account.accountNo:s1111
customer.account.amount:9999.99
加载Yaml文件
创建PropertySourceFactory类
/**
* 用于@PropertySource加载yaml文件
*
* @author younge
* @date 2020/09/18
*/
public class PropertySourceFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null) {
return super.createPropertySource(name, resource);
}
List<PropertySource<?>> sources =
new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
}
}
在上面 properties 的基础上修改如下
@PropertySource(value = {"classpath:customer.yml"}, factory = PropertySourceFactory.class)
@ImportResource 导入Spring 配置文件
1、@ImportResource 注解用来导入 Spring 的配置文件,如核心配置文件 "beans.xml",从而让配置文件里面的内容生效;
2、如果应用中仍然想采用以前 xml 文件的配置方式,如 "beans.xml" ,则使用 “@ImportResource” 注解轻松搞定。
3、将 @ImportResource 标注在一个配置类上,通常直接放置在应用启动类上,和 @SpringBootApplication 一起即可。
4、然后就可以在类路径下提供原始的 beans.xml 配置文件:
5、启动应用控制台会打印:loading XML bean definitions from class path resource [beans. xml] 表示加载成功。
多个配置文件
当项目有多个配置文件的时候,可以用application-***命名,在application.yml/application.properties中配置项目使用激活这些配置文件即可。多个文件名只需要写applicaiton-之后的名称,多个文件名之间用,分割
spring:
profiles:
active: pro,def
这种用法通常也用作多个环境配置文件的切换
application.properties
application-pro.properties
application-dev.properties
application-sit.properties
application-uat.properties
log4j.xml
log4j-pro.xml
log4j-dev.xml
log4j-sit.xml
log4j-uat.xml