SpringBoot读取配置文件参数

方法一

1
2
3
4
#配置文件
3311111111111152=hzlxywrcb
3311111111111161=hzlxlcnsh
3311111111111163=hzlxjhyh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 public static Properties signProperties = new Properties();

static {
try {
// 用当前类的 类加载器 的 getResouceAsStream 读取文件中的内容
InputStream inputStream = ValidateUtils.class.getClassLoader().
getResourceAsStream("properties/signparam.properties");
// load 方法读取 属性列表,机键值对列表
signProperties.load(inputStream);
} catch (Exception e) {
log.error("[ValidateUtils] 配置文件 signparam.properties 读取错误", e);
}
}
// 使用signProperties.getProperty方法去获取配置文件的值
// 比如src=3311111111111152 获取值为hzlxywrcb
String SRCKey = signProperties.getProperty(msgRechargeBean.getSrc());

方法二

1
2
@Value("${providers.lj.id}")
private String providerId;

方法三

1
2
3
4
5
## 配置文件
callback:
yxjfurl: @@@@@@@
rygurl: XXXXXXXX
hfyurl: #######
1
2
3
4
5
6
7
@Data
@ConfigurationProperties(prefix = "callback")
public class CallBackUrlParam {
private String yxjfurl;
private String rygurl;
private String hfyurl;
}
1
2
3
// 引入 使用它的get方法即可获取对应的属性
@Autowired
private CallBackUrlParam callBackUrlParam;
1
2
3
4
5
6
7
8
9
10
11
12
13
// 添加EnableConfigurationProperties注释即可
@EnableConfigurationProperties({CallBackUrlParam.class})
public class EgiftAppApplication {
@PostConstruct
public void start() {
TimeZone.setDefault(TimeZone.getTimeZone(Constant.DEFAULT_TIME_ZONE));
}

public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone(Constant.DEFAULT_TIME_ZONE));
SpringApplication.run(EgiftAppApplication.class, args);
}
}
赏个🍗吧
0%