除了application.yml ,自定义另外的配置文件,如何读取。
bus.yml:
email: xxxx@163.com
scheduleEnable: true
定义配置文件读取类:
/**
bus.yml 参数读取
* @author xz
*
*/
@Component
@PropertySource("classpath:bus.yml")
@ConfigurationProperties
public class BusinessConfig {
private String email;
private boolean scheduleEnable;
public boolean isScheduleEnable() {
return scheduleEnable;
}
public void setScheduleEnable(boolean scheduleEnable) {
this.scheduleEnable = scheduleEnable;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
使用:
@Autowired
BusinessConfig busConfig;
// 正常使用
本文介绍如何在Spring Boot项目中自定义配置文件并实现其读取。通过定义`BusinessConfig`类并使用`@PropertySource`注解指定配置文件路径,再结合`@ConfigurationProperties`注解,可以轻松地将配置文件中的属性映射到Java Bean中。文中还展示了如何通过@Autowired注入配置类并在应用中使用。
624





