Spring boot几种读取多环境配置文件方式

Properties配置文件注入

如果我们还在使用相对较老的Properties文件的方式,存放我们项目的配置信息,那么我们可以通过*@PropertySource*注解、配置@Value的方式读取我们resource目录下的配置文件;

yaml 复制代码
//applicaiton.properties配置
spring.redis.host: 127.0.0.1
spring.redis.port: 3306
spring.redis.password :123456
kotlin 复制代码
@Component
@PropertySources(value = "classpath:application.properties")
public class RedisProperties {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private String port;
    @Value("${spring.redis.password}")
    private int password;
}

如果不想写@Value注解,可以在类头加@ConfigurationProperties(prefix = "spring.redis")注解;

不同环境文件的读取

Spring boot项目是优先读取 application.yml 或者 application.properties,profile机制会根据不同环境读取不同的配置文件

ini 复制代码
//需要我们在applicaiton.properties设置环境变量
spring.profiles.actives = dev

Yml的方式注入

如果使用yml的方式配置文件,如何实现动态注入呢?

yaml 复制代码
//application.yml
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: password

YamlPropertiesFactoryBean类读取

可以采用Spring beans包下对yml文件配置读取的类,YamlPropertiesFactoryBean,还有一个YamlMapFactoryBean返回map集合类型的配置;

csharp 复制代码
    @GetMapping("getRedisConfig")
    public Properties getRedisConfig(){
        YamlPropertiesFactoryBean yamlProFb = new YamlPropertiesFactoryBean();
        yamlProFb.setResources(new ClassPathResource("redis-file.yml"));
        Properties properties = yamlProFb.getObject();
        System.out.println(properties.get("redis.host"));
        System.out.println(properties.get("redis.port"));
        System.out.println(properties.toString());
        return properties;
    }

PropertySourcesPlaceholderConfigurer

也可以采用PropertySourcesPlaceholderConfigurer自定义配置类,使用该类进入属性注入,这个类主要解决的是YamlPropertiesFactoryBean只限于在一个接口使用,如果在定义一个接口不用YamlPropertiesFactoryBean去加载配置类,是无法读取的; 所以我们用PropertySourcesPlaceholderConfigurer,该类是实现Aware接口,而最根本还是调BeanFactoryPostProcessor,bean工厂的后置处理器;

所以我们这么配置,就可以实现全局的配置文件属性读取;

java 复制代码
@Configuration
public class YmlRedisConfig {
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer yamlConfigurer(){
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("redis-file.yml"));
        configurer.setProperties(Objects.requireNonNull(yaml.getObject()));
        return configurer;
    }
}

Environment实现属性动态注入

Spring boot在启动的时候,会构建一个org.springframework.core.env.Environment对象,用于存储配置类; 根据下图,我们可以看到最终都会去实现PropertyResolver接口;

image:2C39516B-C70F-485E-9F88-A4A7CA7F4292-2581-00001ED1E5808FF4/4CE2B56C-5B64-4756-B38A-18BA819200E1.png

java 复制代码
@Autowired
private Environment env;

@Bean
public DataSource dataSource() throws PropertyVetoException, SQLException {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(env.getProperty("mysql.connection.url"));
    dataSource.setUsername(env.getProperty("mysql.connection.username"));
    dataSource.setPassword(env.getProperty("mysql.connection.password"));
    return dataSource;
}

输入、输出流原生态注入

InputStreamReader字节流读取application.propertie,Properties.load()方法 进行读取;

java 复制代码
@Bean
public Properties initRedisConfig() throws IOException {
    Properties properties =new Properties();
    InputStreamReader inputStreamReader =new InputStreamReader(
            this.getClass().getResourceAsStream("application.properties"), StandardCharsets.UTF_8);
    properties.load(inputStreamReader);
    return properties;
}
相关推荐
菠菠萝宝9 分钟前
从传统后端到AI智能驱动:Java + AI 生态深度实战技术总结
java·人工智能·ai·llm·知识图谱·ai编程·rag
陈佳梁11 分钟前
java--对象的引用
java·开发语言
OC溥哥99911 分钟前
2D我的世界创造模式网页版正式出炉——《我们的2D创造世界:无限创意,多人同乐》欢迎来到ourcraft.xin网站上玩
后端·python·阿里云·flask·html·游戏程序
wadesir12 分钟前
Java实现遗传算法(从零开始掌握智能优化算法)
java·开发语言·算法
程序媛徐师姐25 分钟前
Java基于SpringBoot的智能城市管理平台,附源码+文档说明
java·spring boot·java springboot·智能城市管理平台·java智能城市管理平台·java智能城市管理·智能城市管理
tryxr31 分钟前
Java抽象类特点、使用方式和应用场景
java·开发语言·向上转型·抽象类·向下转型
潲爺37 分钟前
Java-多线程
java·笔记·学习
李慕婉学姐40 分钟前
【开题答辩过程】以《婴幼儿辅食健康监测与反馈系统》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot
派大鑫wink41 分钟前
Stream 流:简化集合操作的利器
java·开发语言
lkbhua莱克瓦241 小时前
基础-SQL-DQL
java·开发语言·数据库·笔记·mysql·dql