一 、简介
Spring Boot来简化Spring应用开发,是整个Spring技术栈的大整合,J2EE开发的一站式解决方案。
二 、优点
(1)快速创建独立运行的Spring项目以及与主流框架集成
(2)使用嵌入式的Servlet容器,应用无需打成WAR包
(3)starters自动依赖与版本控制
(4)大量的自动配置,简化开发,也可修改默认值
(5)无需配置XML,无代码生成,开箱即用
(6)准生产环境的运行时应用监控
(7)与云计算的天然集成
三、SpringBoot
1 开发环境搭建
springboot父模块引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
或
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring‐boot‐dependencies</relativePath>
</parent>
启动器配置(starters)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2 主程序,主入口类
- 程序从main方法开始运行
- 使用SpringApplication.run()加载主程序类
- 主程序类标注@SpringBootApplication
- @EnableAutoConfiguration是SpringBoot核心注解
- @Import导入所有的自动配置
- @AutoConfigurationPackage定义默认的包扫描规则
- 程序启动扫描加载主程序类所在包以及下面所有子包的组件。
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args){
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
注解讲解
@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}
@SpringBootConfiguration:Spring Boot的配置类;
标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解;
@EnableAutoConfiguration:开启自动配置功能;
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}
将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
@AutoConfigurationPackage:自动配置包
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
}
EnableAutoConfigurationImportSelector:
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
3 工程目录
resources文件夹中目录结构
static:保存所有的静态资源; js css images;
templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认 不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;
4 配置文件
Spring Boot全局的配置文件
application.properties
application.yml
配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;
多个文档用---隔开
5 YAML语法(YAML Ain't Markup Language)
1 YAML基本语法
(1)使用缩进表示层级关系
(2)缩进时不允许使用Tab键,只允许使用空格。
(3)缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
(4)大小写敏感
值得写法:
字面量k:(空格 空格必须有)普通的值v(数字,字符串,布尔)
注意:以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级。
字符串默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
例如 name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
例如 name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
2 YAML支持的三种数据结构
对象:键值对集合
对象的一组键值对,使用冒号分隔。如:username: admin
分行写法:
maps:
k1: v1
k2: v2
行内写法:
maps: {k1: v1,k2: v2}
数组:一组按次序排列的值
分行写法
lists:
- lisi
- zhaoliu
行内写法:
lists: [lisi,zhaoliu]
字面量:单个的、不可再分的值
k1: v1
6 配置文件注入
@Value和@ConfigurationProperties属性注值对比
属性名匹配规则(Relaxed binding)
person.firstName:使用标准方式
person.first-name:大写用-
person.first_name:大写用_
PERSON_FIRST_NAME: 推荐系统属性使用这种写法
@ConfigurationProperties
指定prefix对应的值,向@bean对象属性赋值
@PropertySource配置用法进入spring注解开发(一)查看
配置文件:
person:
lastName: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: v2}
lists: [lisi,zhaoliu]
dog:
name: 小狗
age: 12
javaBean:
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//....
}
导入配置文件配置器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
ConfigurationProperties Validation
支持JSR303进行配置文件值校验;
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
@Email
private String lastName;
}
@ImportResource读取外部配置文件
@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class HelloWorldMainApplication {}
7 配置文件占位符
- 随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
- 占位符获取之前配置的值,如果没有可以是用:指定默认值
person.last‐name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15
8 Profile
1、多Profile文件
在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml
默认使用application.properties的配置;
2、yml支持多文档块方式
server:
port: 8081
spring:
profiles:
active: prod
---
server:
port: 8082
spring:
profiles: dev
---
server:
port: 8083
spring:
profiles: prod
3、激活方式
命令行 --spring.profiles.active=dev
配置文件 spring.profiles.active=dev
jvm参数 –Dspring.profiles.active=dev
9 配置文件加载位置
spring boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件
- file:./config/
- file:./
- classpath:/config/
- classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置;高优先级配置内容会覆盖低优先级配置内容。
也可以通过配置spring.config.location来改变默认配置
还可以对打包好的进行java -jar spring-boot.jar --spring.config.location=...
10 外部配置加载顺序
SpringBoot也可以从以下位置加载配置; 优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置
1.命令行参数
所有的配置都可以在命令行上进行指定
java -jar spring-boot-..-SNAPSHOT.jar --server.port=8087
2.来自java:comp/env的JNDI属性
3.Java系统属性(System.getProperties())
4.操作系统环境变量
5.RandomValuePropertySource配置的random.*属性值
由jar包外向jar包内进行寻找;优先加载带profile
6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
再来加载不带profile
8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件
9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件
10.@Configuration注解类上的@PropertySource
11.通过SpringApplication.setDefaultProperties指定的默认属性
11 自动配置原理
SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration
xxxAutoConfiguration:自动配置类
12 @Conditional派生注解
通过启用 debug=true属性;来让控制台打印自动配置报告