java绑定配置文件_[学习笔记] SpringBoot 的配置文件、yaml语法、配置注入、松散绑定...

配置文件

SpringBoot 有两种配置文件格式,二选一即可,官方推荐 yaml:

application.properties key=value的格式

application.yaml key: value的格式

配置文件位置

SpringBoot会从这四个位置全部加载主配置文件;互补配置。优先级从高到低。

--spring.config.location=F:/application.yaml

/config/application.yaml

/application.yaml

/src/main/resources/config/application.yaml

/src/main/resources/application.yaml

yaml 格式特点

冒号后面必须有一个空格,不能省略

以缩进来控制层级关系,左边对齐的为同一层级

属性和值的大小写敏感

双引号中的特殊字符会转义输出,如"hello n world"会发生换行

双引号中的特殊字符会原样输出,如‘hello n world’所见即所得

配置示例

# src/main/resources/application.properties

server.port=8081

# src/main/resources/application.yaml

server:

port: 8081

# 对象形式

student:

name: zhangsan

age: 18

# 行内对象形式

student: {name: zhangsan,age: 18}

# 数组形式

pets:

- cat

- dog

- pig

# 行内数组形式

pets: [cat,dog,pig]

配置 banner

# src/main/resources/banner.txt

# https://www.bootschool.net/ascii-art

_ _

_(9(9)__ __/^/^__

/o o /_ ____/_/_/_

___, /_ _.' './_ _/_

`---` /_ _/ /_ _|.'_/

/_/ / /_ |/ /

`-' | ';_:' /

/| .'

/_/ |,___.-`', /`'---`

/___/` /____/

注入 yaml 配置文件(方式一)

package com.wu.helloworld.pojo;

@Component

public class Dog {

@Value("阿黄")

private String name;

@Value("18")

private Integer age;

}

@SpringBootTest

class HelloworldApplicationTests {

@Autowired

Dog dog;

@Test

public void contextLoads() {

System.out.println(dog)

}

}

注入 yaml 配置文件(方式二)

org.springframework.boot

spring-boot-configuration-processor

true

package com.wu.helloworld.pojo;

@Component

public class Dog {

private String name;

private Integer age;

//构造函数、get、set、toString 等方法

}

@Component

@ConfigurationProperties(prefix = "person")

public class Person {

private String name;

private Integer age;

private Boolean happy;

private Date birth;

private Map maps;

private List lists;

private Dog dog;

//构造函数、get、set、toString 等方法

}

person:

name: wu_${random.uuid}

age: ${random.int}

happy: false

birth: 2000/01/01

maps: {k1: v1,k2: v2}

lists:

- code

- girl

- music

dog:

name: ${person.dogName:默认名字}_旺财

age: 1

@SpringBootTest

class DemoApplicationTests {

@Autowired

Person person;

@Test

public void contextLoads() {

System.out.println(person);

}

}

注入 properties 配置文件

设置 properties 的编码格式为UTF-8:

File / Settings / File Encodings / Properties Files / UTF-8 && √ Transparent native-to-ascii conversion

# src/main/resources/person.properties

person.name=wu

person.age=18

person.sex=男

@PropertySource(value = "classpath:person.properties")

@Component

public class Person {

@Value("${person.name}") // 从配置文件中取值

private String name;

@Value("#{9*2}") // #{SPEL} Spring表达式

private int age;

@Value("男") // 字面量

private String sex;

}

松散绑定

dog:

first-name: 旺财

age: 3

@Component

public class Dog {

private String firstName; // 可以绑定横杠的配置值

private Integer age;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值