方式一:yaml类型
1.yml配置文件
player:
attribute:
name: wade
age: 18
main-sex: man
list:
- java
- c++
- python
map:
wechat: Mui1010
blogs: http://blog.csdn
git: http://github.com
2.对应的实体类
@Data
@Component
@ConfigurationProperties(prefix = "player.attribute")
public class Player {
private String name;
private Integer age;
private String mainSex;
private List<String> list;
private Map<String, String> map;
}
方式二:propertites类型
1.propertites配置文件
user.userName=root
user.isAdmin=true
user.regTime=2019/11/01
user.isOnline=1
user.maps.kobe=Lakers
user.maps.wade=Heats
user.lists=list1,list2
user.address.tel=15899988899
user.address.name=上海
2.对应的实体类
@Data
@Component
@PropertySource(value = "classpath:/my-configuration.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "user")
public class User {
private String userName;
private boolean isAdmin;
private Date regTime;
private Long isOnline;
private Map<String, Object> maps;
private List<Object> lists;
private Address address;
}
读取配置文件数据
@RestController
@RequestMapping("/config")
public class ConfigController {
@Autowired
private User user;
@Autowired
private Player player;
@RequestMapping("/getUser")
public User getUser() {
return user;
}
@RequestMapping("/getPlayer")
public Player getPlayer() {
return player;
}
}