You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

properties文件环境变量未加载,Redisson配置文件无法读取如何解决?

解决方法

1. 确认环境变量的有效性

  • 先验证系统/容器中是否正确设置了REDIS_MODE环境变量:Linux/macOS终端执行echo $REDIS_MODE,Windows终端执行echo %REDIS_MODE%,确保输出预期值(如standalone、cluster)。
  • 若使用IDE运行,需在IDE的运行配置中手动添加该环境变量,部分IDE不会自动继承系统环境变量。

2. 通过Spring配置类动态生成Redisson配置路径

如果直接在properties文件中使用${REDIS_MODE}占位符不生效,大概率是Hibernate加载配置时,Spring的占位符替换逻辑还未执行。可以通过配置类动态赋值:

import org.hibernate.cache.redisson.RedissonRegionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;

@Configuration
public class HibernateRedissonConfig {

    @Value("${REDIS_MODE}")
    private String redisMode;

    @Bean
    public Properties hibernateProperties() {
        Properties props = new Properties();
        props.setProperty("hibernate.cache.redisson.config", "redisson-" + redisMode + ".yaml");
        return props;
    }
}

3. 检查配置文件的存放路径

  • 确保redisson-xxx.yaml文件放在src/main/resources目录下(或项目指定的资源目录),保证编译后能被加载到classpath中。
  • 若配置文件在外部路径,需使用绝对路径或file:前缀指定,例如file:/opt/config/redisson-${REDIS_MODE}.yaml。

4. 启用Spring占位符解析器(适配老版本Spring Boot)

如果项目使用老版本Spring Boot或自定义了配置加载逻辑,需确保PropertySourcesPlaceholderConfigurer被正确配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
public class PlaceholderConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

5. 改用系统属性传递参数

若环境变量始终无法被读取,可在启动命令中通过系统属性传递值:

java -jar your-app.jar -DREDIS_MODE=standalone

此时properties文件中的${REDIS_MODE}会读取该系统属性的值。


内容的提问来源于stack exchange,提问作者Ganesh Prajapati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 18:14:55