Application.properties配置文件
(一)创建Spring Boot的Web项目PropertiesDemo
利用Spring Initializr方式创建项目
在application.properties里添加相关配置
点开resource目录,查看应用程序属性配置文件
#修改tomcat默认端口号
server.port=8888
#修改web虚拟路径
server.servlet.context-path=/lzy
运行项目,查看结果
对象类型的配置与使用
(1)创建Pet类
package net.lhf.lesson03.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 功能:宠物类
* 作者:李红芙
* 日期:2021年04月28日
*/
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类
package net.lhf.lesson03.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* 功能:人类
* 作者:李红芙
* 日期:2021年04月28日
*/
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 +
'}';
}
}
在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=瑞瑞
给Person类添加注解
添加注解@Component,交给Spring去管理
添加注解@ConfigurationProperties(prefix = “person”)
未配置注解处理器,暂时不管,看它一会会不会影响运行
给Pet类添加注解
添加注解@Component,交给Spring去管理
添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加
从Spring容器里获取Person类的实例并输出
实现接口ApplicationContextAware,实现其抽象方法setApplicationContext
package net.lhf.lesson03;
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;
}
}
运行测试,出现乱码
解决输出结果的汉字乱码问题
使用JDK工具native2ascii.exe将汉字处理成uncode编码
Microsoft Windows [版本 10.0.18363.1379]
(c) 2019 Microsoft Corporation。保留所有权利。
D:\java\JSP\PropertiesDemo>cd src/main/resources
D:\java\JSP\PropertiesDemo\src\main\resources>native2ascii -encoding utf8 application.properties
#\u4fee\u6539tomcat\u9ed8\u8ba4\u7aef\u53e3\u53f7
server.port=8888
#\u4fee\u6539web\u865a\u62df\u8def\u5f84
server.servlet.context-path=/lzy
#\u914d\u7f6e\u5bf9\u8c61
person.id=1
person.name=\u674e\u7ea2\u8299
person.hobby=\u65c5\u6e38,\u7f8e\u98df,\u97f3\u4e50
person.family.father=\u674e\u5ead\u91d1
person.family.mother=\u5218\u53d1\u6885
person.family.grandpa=\u4e07\u9f99\u82b3
person.famliy.grandma=\u5218\u4e07
person.family.son=\u674e\u6b46
person.family.daughter=\u674e\u9ece
person.pet.type=\u91d1\u6bdb
person.pet.name=\u745e\u745e
D:\java\JSP\PropertiesDemo\src\main\resources>
修改application.properties文件,汉字采用unicode编码形式
运行测试类,查看结果
从Spring容器里获取Pet类的实例并输出
查看Pet类的注解,有配置属性的注解@ConfigurationProperties(prefix = “person.pet”)
在测试类里添加测试方法testPet()
运行测试
去掉Pet类的配置属性的注解@ConfigurationProperties(prefix = “person.pet”)
运行测试方法testPet(),查看结果
大家可以看到,宠物对象的属性依然没有被注入,下面我们换一种属性注解的方式,采用@Value注解方式。
给Pet类的属性添加值注解@Value
再次运行测试方法testPet(),查看结果,出现错误
两种属性注解方式的对比
采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为所注解的类的全部属性注入相应的值,包括简单类型和复杂类型(List、Map、Pet……)。
采用@Value注解方式,优点在于可以不要set方法,但是有两点不足:其一、需要一个一个地注入,显得麻烦;其二、对于复杂类型不能注入,比如Map、List、Pet等。
Application.yaml配置文件
1、备份application.properties文件
文件更名为application.back,即让此文件不起作用
在resoures目录里创建application.yaml文件
创建application.yaml文件
配置服务器属性
运行测试方法contextLoads(),查看结果