1.自定义配置文件config.properties。
config.servicepath=http\://127.0.0.1\:8080
config.author = lingqingyu
2.定义一个Bean,Bean中的属性和配置文件中的属性保持一致。
public class ConfigBean {
private String servicepath;
private String author ;
public String getServicepath() {
return servicepath;
}
public void setServicepath(String servicepath) {
this.servicepath = servicepath;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
3.在该类上添加项目启动是需要加载的配置文件的路径。
@Component
@PropertySource(value= "classpath:/config.properties")
@ConfigurationProperties(prefix = "config")
4.在项目的启动类上添加
@EnableConfigurationProperties(ConfigBean.class)
5.注解ConfigBean,使用get方法可拿到定义的属性值