1.获取文件内容就要先有文件
2.内容比如有
3定义配置相关变量的类
内部代码:
package com.haha.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix="dali")
@PropertySource("classpath:config.properties")
public class ConfigProperties {
String name;
String msg;
String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
注解一定不要搞错
先是@Configuration
其次@ConfigurationProperties(prefix=“dali”) “dali”就是你定义的配置文件的变量名如下:
然后@PropertySource(“classpath:config.properties”) 路径
然后定义相关变量是name,msg,password get和set一下
3.启动类声明对象
封装对象,获取相关变量就行了
4.启动输出