@ConfigurationProperties(prefix = “person”) 需要set
package com.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person") //将配置文件中以person开头的属性注入到该类中
public class Person {
private int id; //id
private String name; //名称
private List hobby; //爱好
private String[] family; //家庭成员
private Map map;
private Pet pet; //宠物
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", hobby=" + hobby +
", family=" + Arrays.toString(family) +
", map=" + map +
", 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 String[] getFamily() {
return family;
}
public void setFamily(String[] family) {
this.family = family;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
}
package com.demo.pojo;
public class Pet {
private String type; //类型
private String name; //名字
@Override
public String toString() {
return "Pet{" +
"type='" + type + '\'' +
", name='" + 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;
}
}
application.yml
# 对实体对象person进行属性配置
person:
id: 2
name: lucy
hobby: [吃饭,睡觉,打豆豆]
family: [father,mother]
map: {k1: v1,k2: v2}
pet: {type: dog,name: 旺财}
@Value 注入 不需要set
@Component
public class Student {
@Value("${person.id}")
private int id;
@Value("${person.name}")
private String name;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
@PropertySource 加载自定义配置文件 testa.properties
@Component
@PropertySource("classpath:testa.properties") //配置自定义配置文件的名称及位置
@ConfigurationProperties(prefix = "test")
public class MyProperties {
private int id;
private String name;
@Override
public String toString() {
return "MyProperties{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
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;
}
}
testa.properties
test.id=1
test.name=wangwu
@Configuration @Bean 编写自定义配置类
@Configuration //标明该类为配置类
public class MyConfig {
@Bean(name = "iservice") //将返回值对象作为组件添加到spring容器中,标识id默认是方法名
public MyService myService() {
return new MyService();
}
}
public class MyService {
}
随即值设置
my.secret=${random.value} //配置随机值
my.number=${random.int} //配置随机整数
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)} //配置小于10的随机整数
my.number.in.range=${random.int[1024,65536]} //[1024, 65536]直接的随机整数
参数间引用
app.name=MyApp
app.description=${app.name} is a Spring Boot application
启动配置指定
Spring-boot 启动 制定配置文件
java -jar demo.jar --spring.config.location=application.properties