• 【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 对象读取。

  • 相关阅读:
    基于文本信息抽取的列控车载设备故障发现
    webpack核心模块
    ant design vue对话框关闭数据清空
    软件架构思想和系统架构图
    私有云笔记推荐(obsidian+nas同步)
    Golang http 请求如何设置代理
    【linux命令讲解大全】002. 使用locate更快速地查找文件
    WinForm,可能是Windows上手最快的图形框架了
    双链表的基本操作
    在Ubuntu系统上部署Inis博客,并使用内网穿透将博客网站发布到公共互联网上
  • 原文地址:https://blog.csdn.net/sco5282/article/details/126606077