|
SpringBoot配置文件详解[云图智联]1
发表时间:2020-07-02 11:52 免费学习视频欢迎关注云图智联:https://e.yuntuzhilian.com/ 配置文件详解 application.properties properties配置文件我们之前都有接触,今天就让我们来认识一下yaml配置文件吧。
YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。 YAML基本语法
port: 8081 path: /example 字面量:普通的值(整数、浮点数、字符串、布尔、Null值、时间、日期) key: value(字面值直接写上就可以)
boolean: TRUE #true,True都可以 float: - 3.14 - 6.8523015e+5 #可以使用科学计数法 int: - 123 - 0b1010\_0111\_0100\_1010\_1110 #二进制表示 null: nodeName: 'node' parent: ~ #使用~表示null string: - 哈哈 - 'Hello world' #可以使用双引号或者单引号包裹特殊字符 - newline newline2 #字符串可以拆成多行,每一行会被转化成一个空格 date: - 2018-02-17 #日期必须使用ISO 8601格式,即yyyy-MM-dd user: username: root password: rootpwd user:{username: root,password: rootpwd} 示例: pets: - cat - dog pets: \[cat,dog\] 注释 文本块 value: | hello world! +表示保留文字块末尾的换行,-表示删除字符串末尾的换行。 value: | hello value: |\- hello value: |+ hello \# 输出:hello\\n hello hello\\n\\n(有多少个回车就有多少个\\n) >:使用 > 标注的文本内容缩进表示的块,将块中回车替换为空格,最终连接成一行 value: > hello world! \# 输出:hello 空格 world! \## 测试ConfigurationProperties user: userName: root isAdmin: true regTime: 2020/01/01 isOnline: 1 maps: {k1 : v1,k2: v2} lists: - list1 - list2 address: tel: 15899988899 name: 上海市 2.1 User类 import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
public String toString() { public String getUserName() { public void setUserName(String userName) { public boolean isAdmin() { public void setAdmin(boolean admin) { public Date getRegTime() { public void setRegTime(Date regTime) { public Long getIsOnline() { public void setIsOnline(Long isOnline) { public Map<String, Object> getMaps() { public void setMaps(Map<String, Object> maps) { public List<Object> getLists() { public Address getAddress() { } 需要将配置文件中每个属性都映射到这个组件,注意:记得加@Component注解,这样才能将这个组件加载到Spring容器中 @ConfigurationProperties(prefix = "user") 通过prefix属性配置的user在配置文件中绑定user对象,将配置文件中的属性值直接映射到bean属性中 2.2 Address类 public class Address { private String tel; private String name; @Override public String toString() { return "Address{" + "tel='" + tel + '\'' + ", name='" + name + '\'' + '}'; } public String getTel() { return tel; } public void setTel(String tel) { this.tel= tel; } public String getName() { return name; } public void setName(String name) { this.name= name; } } ok,这时候启动项目的时候就可在容器中直接使用user对象了,现在我们使用spirng自带的测试类,测试一下 import com.example.easy.entity.Stu; @Autowired System.out.println(user); } } 说明: 运行效果 免费学习视频欢迎关注云图智联:https://e.yuntuzhilian.com/
文章分类:
SpringBoot
|