序
当读完我们的spring boot如何搭建之后,我们可以看到spring boot的目录结构很简单:
在src下面就一个java文件包和一个resources,进入resource之后呢。
它也就是一个application.properties这样的配置文件。
我们可以带着下面几个疑问来看本篇文章的描述:
1.spring boot的配置文件为什么能够改成yml?它是如何实现.properties可以&&.yml也是可以的呢?
2.spring boot的配置文件中的数据怎么读取到?
3.为什么spring boot在idea中写yml文件或者是写.properties文件能够自动提示出来?是idea的自动提示?如果是那么idea自动提示的源在哪里?
4.如何做到spring boot的多配置(生产,开发)?
5.如何做到spring boot外部配置(难,不属于基础范围)?
1.spring boot的配置文件为什么能够改成yml?
我们生成的.properties文件是可以被改变成为.yml文件的。
.properties的文件格式是这样的:
server.port=8080
server.session-timeout=30
server.context-path=
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8
spring.datasource.url = jdbc:mysql://localhost:3306/spring
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
.yml的配置文件如下:
server:
port: 8080
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
spring:
datasource:
url : jdbc:mysql://localhost:3306/springboot
username : root
password : root
driverClassName : com.mysql.jdbc.Driver
jpa:
database : MYSQL
show-sql : true
hibernate:
ddl-auto : update
naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect : org.hibernate.dialect.MySQL5Dialect
现在来描述下原因:
SpringBoot 的配置文件内置支持 properties、xml、yml、yaml 几种格式,其中 properties和xml 对应的Loader类为 PropertiesPropertySourceLoader ,yml和yaml 对应的Loader类为 YamlPropertySourceLoader。
观察这2个类可以发现,都实现自接口 PropertySourceLoader 。所以我们要新增支持别的格式的配置文件,就可以通过实现接口 PropertySourceLoader 来实现了。
可以在源码里面搜索PropertySourceLoader这个类,就可以看到实现这两个类的有PropertiesPropertySourceLoader,YamlPropertySourceLoader,如果看过自定义starter的文章的同学们就可以了解到,我们点击源码可以看到
org.springframework.boot.env.PropertySourceLoader=\ org.springframework.boot.env.PropertiesPropertySourceLoader,\ org.springframework.boot.env.YamlPropertySourceLoader
配置文件加载的方式有两种,所以所,一个项目中定义yml文件和properties文件都可以成为spring boot的配置文件的读取地址。
2.spring boot是如何读取到里面的数据的?
可以看到,主要的就是由上面的PropertiesPropertySourceLoader,YamlPropertySourceLoader两个资源文件加载的文件资源。
3.如何做到spring boot的配置文件的生产和开发?
直接上代码:
新建一个application-dev.yml,新建一个application-prod.yml两个文件。然后在application.yml中引入需要的yml配置文件就可以做到生产和开发环境的切换了:
spring: profiles: active: dev
注:如何在linux上面不需要修改代码里面的application.yml中的环境动态切换生产和开发环境:
# 在linux的环境上面如果要切换环境,其实可以不要修改这个配置文件,用以下命令就可以将 # java -jar spring-boot-application-properties-sample-1.0.0.jar --spring.profiles.active=dev # java -jar -Dspring.profiles.active=dev1 spring-boot-application-properties-sample-1.0.0.jar # 命令行参数的优先级大于java系统参数 # 例子:java -jar -Dspring.profiles.active = dev1 spring-boot-application-properties-sample.jar -- spring.profiles.active = dev2 # 上面的命令是生效与 dev2生效,dev1被覆盖