1. SpringBoot配置文件类型
1.1 SpringBoot配置文件类型和作用
SpringBoot是基于约定的,如果想要自定义配置来替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)来进行配置。
SpringBoot默认会从Resource目录下加载application.properties或者application.yml(application.yaml)文件。application.properties文件是键值对类型的文件,我们经常使用,这里就不做过多说明。我们主要分析一下yml文件的配置。
1.2 application.yml配置文件
1.2.1 yml配置文件介绍
yml文件的格式是YAML,YAML是一种直观的能够被电脑识别的数据序列化格式,容易被阅读,容易和脚本语言进行交互,可以被支持YAML库的不同编程语言程序导入(如C/C++,Ruby,Python,Java,Perl,C#,PHP等)。yml文件是以数据为核心的,比传统的xml方式更加简洁。
yml文件的扩展名可以是.yml或者.yaml。
1.2.2 yml配置文件的语法
(1) 配置普通数据
语法:key: value
#普通数据
name: by2
注意:value之前有一个空格
(2)配置对象数据
语法:
key:
key1: value1
key2: value2
或者:
key:{key1: value1,key2: value2}
#配置对象数据
idol:
name: by2
age: 27
addr: beijing
#或者(常用上面那种)
#person: {name: by2,age: 27, addr: beijing}
注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别
(3)配置map数据
同上面的对象语法。
(4)配置数组(List、Set)数据
语法:
key:
- value1
- value2
或者
key:{value1,value2}
#配置数组
image:
- yumi
- miko
- yumiko
#或者(常用上面那种)
#image: [yumi,miko,yumiko]
#元素是对象形式
by2:
- name: yumi
age: 27
cname: '甜玉米'
- name: miko
age: 27
cname: '霸气糕'
注意:value1与之前的 - 之间存在一个空格
1.3 SpringBoot配置信息的查询
SpringBoot官方文档:https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#boot-features-kotlin-configuration-properties
我们如果要修改SpringBoot的默认配置,可以查阅官方文档查询相关的key值。
常用的配置摘抄如下:
# QUARTZ SCHEDULER (QuartzProperties)
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode. spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@. sql # Path to the SQL file to use to initialize the database schema.
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
# ---------------------------------------- #
WEB PROPERTIES
# ----------------------------------------
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080 # Server HTTP port.
server.servlet.context-path= # Context path of the application.
server.servlet.path=/ # Path of the main dispatcher servlet.
# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
# JACKSON (JacksonProperties)
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
# SPRING MVC (WebMvcProperties)
spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher servlet.
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto- detected based on the URL by default. spring.datasource.password= # Login password of the database.
spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
# JEST (Elasticsearch HTTP client) (JestProperties)
spring.elasticsearch.jest.password= # Login password.
spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use.
spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use.
spring.elasticsearch.jest.read-timeout=3s # Read timeout.
spring.elasticsearch.jest.username= # Login username.
2. 配置文件与配置类的属性映射方式
2.1 使用注解@Value映射
可以通过@Value注解将配置文件中的值映射到一个Spring管理的Bean的字段上
例如:
application.yml配置如下:
idol:
name: by2
age: 27
addr: beijing
实体Bean代码如下:
@Controller public class Quick2Controller { @Value("${idol.name}") private String name; @Value("${idol.age}") private String age; @RequestMapping("/quick2") @ResponseBody public String quick2(){ return "name:" + name + ",age:" + age; } }
浏览器访问结果:
2.2 使用注解@ConfigurationProperties映射
通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射
例如:
application.yml配置同上不变。
实体Bean代码如下:
@Controller @ConfigurationProperties(prefix = "idol") public class Quick2Controller2 { private String name; private String age; private String addr; @RequestMapping("/quick3") @ResponseBody public String quick3(){ return "name:" + name + ",age:" + age + ",addr" + addr; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }
浏览器访问结果如下:
注意:使用@ConfigurationProperties方式进行配置文件与实体字段的自动映射时,需要提供字段的set方法才可以。而使用@Value注解时不需要提供set方法。