• 【SpringBoot】SpringBoot 读取配置文件中的自定义属性的 5 种方法


    SpringBoot 配置文件的格式有两种:yml、properties。有些方法都适用,但有些方法就有针对性了。

    概括地说,Spring Boot 中读取配置文件有以下 5 种方法:

    1. 使用 @Value 读取配置文件(yml、properties)
    2. 使用 @ConfigurationProperties 读取配置文件(yml、properties)
    3. 使用 Environment 读取配置文件(yml、properties)
    4. 使用 @PropertySource 读取配置文件(yml、properties)
    5. 使用 原生方式 读取配置文件(properties)

    如:配置文件内容:

    yml 格式:

    file:
      uploadPath: E:/upload
    
    • 1
    • 2

    properites 格式:

    file.uploadPath=E:/upload
    
    • 1

    1、 使用 @Value 读取配置文件

    @SpringBootApplication
    public class Application implements InitializingBean{
    
    	@Value("${file.uploadPath}")
    	private String profileName;
    
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    
    	@Override
    	public void afterPropertiesSet() throws Exception {
    		System.out.println(profileName);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2、使用 @ConfigurationProperties 读取配置文件

    @Data
    @Component
    @ConfigurationProperties(prefix = "file")
    public class FileConfig {
        // 上传路径
        private String uploadPath;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3、使用 Environment 读取配置文件

    将此类使用 @Autowired 注入到类中就可以使用它的 getProperty() 方法来获取某个配置项的值了

    @SpringBootApplication
    public class Application implements InitializingBean{
    
    	@Autowired
    	private Environment environment;
    
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    
    	@Override
    	public void afterPropertiesSet() throws Exception {
    		System.out.println(environment.getProperty("file.uploadPath"));
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4、使用 @PropertySource 读取配置文件
    使用 @PropertySource 注解可以用来指定读取某个配置文件,比如指定读取 application.yml 配置文件的配置内容

    @SpringBootApplication
    @PropertySource("classpath:application.yml")
    public class Application implements InitializingBean{
    
    	@Value("${file.uploadPath}")
    	private String path;
    
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    
    	@Override
    	public void afterPropertiesSet() throws Exception {
    		System.out.println(path);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5、使用 原生方式 读取配置文件(properties)

    就是使用 Properites 对象读取。

  • 相关阅读:
    【论文阅读】通过3D和2D网络的交叉示教实现稀疏标注的3D医学图像分割(CVPR2023)
    城市燃气价格体系及计量、计费
    SpringMVC学习笔记(二)
    《开源大模型食用指南》全网发布,轻松助你速通LLM大语言模型!
    docker安装redis
    2022-08-20 网易秋招笔试
    online java
    服务器配置openvpn,ssh连接断开
    使用模拟退火(SA)和Matlab的车辆路径问题(VRP)(Matlab代码实现)
    Day14--商品列表-点击商品item项导航跳转到商品详情页面
  • 原文地址:https://blog.csdn.net/sco5282/article/details/126606077