一、Application.properties配置文件
1、在application.properties里添加相关配置
- 点开resource目录,查看应用程序属性配置文件
- 配置tomcat端口号和web虚拟路径
#修改tomcat默认端口号
server.port=8888
#修改web虚拟路径
server.servlet.context-path=/lzy
2、对象类型的配置与使用
(1)创建Pet类
package lesson3;
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 + '\'' +
'}';
}
}
(2)创建Person类
package lesson3;
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 hobby; //爱好
private Map 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 getHobby() {
return hobby;
}
public void setHobby(List hobby) {
this.hobby = hobby;
}
public Map getFamily() {
return family;
}
public void setFamily(Map 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 +
'}';
}
}
(3)在application.properties里配置对象
#配置对象
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=瑞瑞
(4)给Person类添加注解
- 注意:采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为Person类所有属性注入相应的值,包括简单类型和复杂类型
(5)给Pet类添加注解
(6)获取person类实列并输出
package lesson3;
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 Lesson3ApplicationTests implements ApplicationContextAware {
private static ApplicationContext context;
@Test
void contextLoads() {
Person person = (Person) context.getBean("person");
System.out.println(person);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
}
- 运行测试类
(7)解决输出结果的汉字乱码问题
- 使用JDK工具native2ascii.exe将汉字处理成uncode编码
- 修改application.properties文件,汉字采用unicode编码形式
#配置对象
person.id=1
person.name=\u5f20\u4e09\u4e30
person.hobby=\u65c5\u6e38,\u7f8e\u98df,\u97f3\u4e50
person.family.father=\u5f20\u4e91\u5149
person.family.mother=\u5434\u6587\u71d5
person.family.grandpa=\u5f20\u5b8f\u5b87
person.famliy.grandma=\u5510\u96e8\u6b23
person.family.son=\u5f20\u541b\u5b9d
person.family.daughter=\u5f20\u6653\u654f
person.pet.type=\u6cf0\u8fea\u72ac
person.pet.name=\u745e\u745e
- 运行结果
(8)从Spring容器里获取Pet类的实例并输出
- 在测试类里添加测试方法testPet()
- 运行测试
(9)给Pet类的属性添加值注解@Value
- 配置application.properties
- 运行结果:
3、两种属性注解方式的对比
- 采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为所注解的类的全部属性注入相应的值,包括简单类型和复杂类型(List、Map、Pet……)。
- 采用@Value注解方式,优点在于可以不要set方法,但是有两点不足:其一、需要一个一个地注入,显得麻烦;其二、对于复杂类型不能注入,比如Map、List、Pet等。
二、Application.yaml配置文件
1、在resoures目录里创建application.yaml文件
#配置服务器
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: 瑞瑞
- 运行测试方法contextLoads()
- 运行测试方法testPet()
三、两种配置文件的比较
1、application.properties配置文件
- 采用XML语法,键值对:键=值,没有层次结构
- 如果值里有汉字,必须得转成unicode,否则会出现乱码问题
2、application.yaml配置文件
- 采用YAML语法,键值对:键: 值(冒号与值之间有空格),具有层次结构
- 允许值里有汉字,不必转成unicode,也不会出现乱码问题