springboot 读取配置文件。包含普通方法、静态方法、打完jar包配置文件路径改变

springboot 读取配置文件。包含普通方法、静态方法、打完jar包配置文件路径改变,第1张

我的配置文件application.yml如下:

一、读取核心配置文件
  核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties内容如下:

server.port=9090
test.msg=Hello World Springboot!

1、使用@Value方式(常用):
直接在controller或者实现类当中使用注解就可以

@RestController
public class WebController {
    @Value("${test.msg}")
    private String msg;
}

2、使用Environment方式
这个方式没有实际使用过,我写上报错,也没深究锅,这里就不阐述

3、读取出来一个文件appliation.yml文件。使用propertory

4、使用ConfigurationProperties注解
我写了一个工具类,使用注解后,读取到了application.yml
注意一定要小写不然报错,代码如下:

@Component
@ConfigurationProperties(prefix = "ftp")
public class FTPConfig {
    private String ADDRESS;
    private Integer PORT;
    private String USERNAME;
    private String PASSWORD;
    private String BASEPATH;
    private String BASEURL;
..........}

调用的时候:和普通的调用一样,先@Autowired
@Autowired
private FTPConfig FTPConfig;
方法中直接调用

  public static BaseResponse pictureUpload(MultipartFile uploadFile, int code, int type) {
        try {
            String HOST = fileUTils.FTPConfig.getADDRESS();
            Integer PORT = fileUTils.FTPConfig.getPORT();
            String USERNAME = fileUTils.FTPConfig.getUSERNAME();
            String PASSWORD = fileUTils.FTPConfig.getPASSWORD();
            String BASEPATH = fileUTils.FTPConfig.getBASEPATH();
            String BASEURL = fileUTils.FTPConfig.getBASEURL();
            }}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/733963.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存