springboot 有两种配置文件格式,二选一即可,官方推荐 yaml:
springboot会从这四个位置全部加载主配置文件;互补配置。优先级从高到低。
# 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]
# src/main/resources/banner.txt # https://www.bootschool.net/ascii-art _ _ _(9(9)__ __/^\/^\__ /o o \/_ __\_\_/\_/_/_ \___, \/_ _\.' './_ _/\_ `---`\ \/_ _\/ \/_ _|.'_/ \ \/_\/ / \/_ |/ / \ `-' | ';_:' / /| \ \ .' /_/ |,___.-`', /`'---` /___/` /____/
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) } }
<!-- 导入配置文件处理器依赖,需要重启ide --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional>true</optional> </dependency>
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<string,object> maps; private list<object> 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 的编码格式为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; }
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论