Spring Boot加载自定义配置文件
1、创建Spring Boot Web项目ConfigDemo01
设置项目元数据
2、创建自定义配置文件
在resources下创建myconfig.properties文件
3、创建自定义配置类
在net.lhf.lesson04.config包里创建配置类StudentConfig
package net.lhf.lesson04.config;
/**
* 功能:学生配置类
* 作者:李红芙
* 日期:2021年5月7日
*/
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component //让Spring容器来管理Bean
@PropertySource("classpath:myconfig.properties") //加载自定义配置文件
@ConfigurationProperties(prefix = "student") // 此注解必须要Component注解
public class StudentConfig {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge<