首页 > 其他分享 >SpringBoot系列——加载自定义配置文件

SpringBoot系列——加载自定义配置文件

时间:2022-09-28 22:23:54浏览次数:63  
标签:自定义 配置文件 private telephones employee properties SpringBoot

Java开发过程中,一些配置信息不想写到application.properties里面去,想自己弄一个配置文件,然后加载。例子如下:

Employee.java类核心代码:

@Configuration//用来标注一个自定义的配置类,该类会作为Bean组件被添加到Spring容器中,其作用等同于@Compenent
  //  @Component
@PropertySource("classpath:employee.properties")//指定自定义配置文件的位置和名称
@ConfigurationProperties(prefix = "employee")//指定配置文件注入属性的前缀
@Data
public class Employee {
    private String empId;//员工编号
    private String empName;
    private List<String> hobbies;
    private Map<String,String> telephones;
    private int salary;
}

employee.properties内容:
employee.empId=11111
employee.emp-name=名字
employee.hobbies=[唱歌,跳舞]
employee.telephones.mobile=18913816282
employee.telephones.tel=025-52111789
employee.salary=10000

 

标签:自定义,配置文件,private,telephones,employee,properties,SpringBoot
From: https://www.cnblogs.com/Guest999/p/16739777.html

相关文章