赞
踩
Spring Boot使用一个application.properties或application.yml/application.yaml的文件作为全局配置文件,该文件放在 【src/main/resources】目录或者类路径的 【/config】,一般为放在resources目录。我们可以在 application.properties / application.yml文件中定义Spring Boot定义项目的相关属性,、这些属性可以是系统属性、环境变量、命令参数等信息,也可以是自定义配置文件名称和位置。
掌握两个注解
package net.lj.lesson03.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; public class Person { private int id; //编号 private String name; //姓名 private List<String> hobby; //爱好 private Map<String,String> family; //家庭成员 private Pet pet; //宠物 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Map<String, String> getFamily() { return family; } public void setFamily(Map<String, String> family) { this.family = family; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", hobby=" + hobby + ", family=" + family + ", pet=" + pet + '}'; } }
Pet.java
package net.lj.lesson03.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 宠物类 */ public class Pet { private String type; //类型 private String name; //名字 public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Pet{" + "type='" + type + '\'' + ", name='" + name + '\'' + '}'; } }
配置对象
person.id=1
person.name=二哈
person.hobby=拆家,拆楼
person.family.father=哈狗
person.family.mother=傻狗
person.family.grandpa=二狗
person.famliy.grandma=土狗
person.family.son=玛卡巴卡
person.family.daughter=汤不理吧
person.pet.type=哈士奇
person.pet.name=黑子
package net.lj.lesson03; import net.lj.lesson03.bean.Person; import net.lj.lesson03.bean.Pet; import org.junit.jupiter.api.Test; import org.springframework.beans.BeansException; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @SpringBootTest class PropertiesDemoApplicationTests implements ApplicationContextAware {//应用容器感知接口 private ApplicationContext context;//声明应用容器 @Test void contextLoads() { } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } @Test public void testPerson() { //从应用容器里按名称获取person实例 Person person = (Person) context.getBean("person"); //输出Person对象 System.out.println(person); } @Test public void testPet(){ //从应用容器里按名称获取Pet实例 Pet pet = (Pet) context.getBean("pet"); //输出Pet对象 System.out.println(pet); } }
1.打开Terminal面板,输入以下命令
cd src/main/resources
native2ascii -encoding utf8 application.properties
2.将转码的内容复制到application.properties文件
@ConfigurationProperties注解方式,必须要有set方法才会自动为所注解的类的全部属性注入相应的值
@Value注解方式,可以不要set方法,但需要一个一个地注入,比较麻烦,而且对于复杂类型不能注入
#配置服务器 server: port: 8888 servlet: context-path: /lzy #配置person对象 person: id: 1 name: 张三 hobby: 吃东西 跳舞 唱歌 family: { father: 李四, mother: 王五, grandpa: 赵六, grandma: 孙七, son: 王八, daughter: 李二 } pet: type: 斗牛犬 name: 拉拉 #配置pet对象 pet: type: 牧羊犬 name: 大黑
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。