没有备案的网站可信吗,网站解析后显示在建设中,优化网站多少钱,文学网站开发设计报告SpringBoot配置的文件名是固定的#xff1a;application.yml application.properties
YAML:以数据为中心 比Json xml更适合做配置文件 YAML语法:
1 字面量:普通值(字符串 布尔值 数字) (1) k: v (2) 不会转义 会转义
2 对象#xff0c;map(属性和值) (1)…
SpringBoot配置的文件名是固定的application.yml application.properties
YAML:以数据为中心 比Json xml更适合做配置文件 YAML语法:
1 字面量:普通值(字符串 布尔值 数字) (1) k: v (2) 不会转义 会转义
2 对象map(属性和值) (1) k: v (2) 在下一行 写对象的属性和值 数组(List,set) 用 - 表示数组中的一个元素 行内写法 properties配置文件容易乱码 需要在设置中找到这个然后调成这样 示例1(在yml文件中配置):
application.yaml文件中配置
Person:name: 张三age: 21boss: truebirth: 2022/12/5maps: {key1: value1,key2: value2}list: [dog,cat,pig]
Person类中利用ConfigurationProperties(prefix )引入配置类中的元素 但是注意Person类中的类的名字必须与application.yaml中一致
package com.qcby.model;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;Component
ConfigurationProperties(prefix person)
public class Person {private String name;private Integer age;private boolean boss;private Date birth;private MapString,String maps;private ListString list;public Person() {}public Person(String name, Integer age, boolean boss, Date birth, MapString, String maps, ListString list) {this.name name;this.age age;this.boss boss;this.birth birth;this.maps maps;this.list list;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public boolean isBoss() {return boss;}public void setBoss(boolean boss) {this.boss boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth birth;}public MapString, String getMaps() {return maps;}public void setMaps(MapString, String maps) {this.maps maps;}public ListString getList() {return list;}public void setList(ListString list) {this.list list;}Overridepublic String toString() {return Person{ name name \ , age age , boss boss , birth birth , maps maps , list list };}
}最终在Test包下进行测试
RunWith(SpringRunner.class) //声明为测试单元
SpringBootTest //读取配置
public class SpringBoot01ApplicationTests {AutowiredPerson person;Testpublic void Context(){System.out.println(person);}}
示例2(在properties文件中配置)
在application.properties中
person.emaillucky
person.hellojunit
person.lastNamejiangjiayi
person.age12
person.bosstrue
person.listdog,cat
person.maps.key1value1
person.maps.key2value2
person.dog.name${person.hello} //${} 可以理解为Vue中的{{}} 调用的是上面的person.hello的值
person.dog.age16
Person类
package com.qcby.model;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;Component
ConfigurationProperties(prefix person)
public class Person {String email;String hello;String lastName;int age;boolean boss;ListString list;MapString,Stringmaps;Dog dog;public String getEmail() {return email;}public void setEmail(String email) {this.email email;}public String getHello() {return hello;}public void setHello(String hello) {this.hello hello;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName lastName;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public boolean isBoss() {return boss;}public void setBoss(boolean boss) {this.boss boss;}public ListString getList() {return list;}public void setList(ListString list) {this.list list;}public MapString, String getMaps() {return maps;}public void setMaps(MapString, String maps) {this.maps maps;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog dog;}Overridepublic String toString() {return Person{ email email \ , hello hello \ , lastName lastName \ , age age , boss boss , list list , maps maps , dog dog };}
}
Dog类
package com.qcby.model;public class Dog {String name;int age;public Dog() {}public Dog(String name, int age) {this.name name;this.age age;}Overridepublic String toString() {return Dog{ name name \ , age age };}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}
}测试类
RunWith(SpringRunner.class) //声明为测试单元
SpringBootTest //读取配置
public class SpringBoot01ApplicationTests {AutowiredPerson person;Testpublic void Context(){System.out.println(person);}} 如果不是标准的application.properties 是其他的Person类中需要用PropertySource来添加配置文件的路径
ImportSource
导入Spring的配置文件让配置文件里面的内容生效Spring Boot里面没有Spring的配置文件我们自己编写的配置文件也不能自动识别想让Spring的配置文件生效加载进来ImportResource标注在一个配置类上 SpringBoot推荐给容器中添加组件的方式推荐使用全注解的方式1、配置类Configuration------Spring配置文件2、使用Bean给容器中添加组件将方法的返回值添加到容器中 容器中这个组件的默认ID就是方法名 生成随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}多profile文件 通过spring: profiles: active: 来决定运行谁 配置文件加载位置的优先级
–file:./config/ –file:./ –classpath:/config/ –classpath:/优先级由高到底高优先级的配置会覆盖低优先级的配置