-
指定自定义的配置文件bill.properties
-
bill.properites
name=bill
age=11
happy=false
birth=2021/12/2
-
通过@PropertySource注解指定自定义的配置文件@PropertySource(value = "classpath:bill.properties")
-
person类
package com.bill;
import com.bill.Dog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Auther: wangchunwen
* @Date: 2023/1/16 - 01 - 16 - 21:10
* @Description: com.bill.controller
* @version: 1.0
*/
@Component
@PropertySource(value = "classpath:bill.properties")
public class Person {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@Value("${happy}")
private Boolean happy;
@Value("${birth}")
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public Person(){
}
public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
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 getHappy() {
return happy;
}
public void setHappy(Boolean happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
测试结果:
标签:name,age,50,lists,230116,birth,happy,public,SpringBoot From: https://www.cnblogs.com/wcwblog/p/17056479.html