Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring引导-在@Configuration类中使用外部化配置值_Spring_Spring Boot - Fatal编程技术网

Spring引导-在@Configuration类中使用外部化配置值

Spring引导-在@Configuration类中使用外部化配置值,spring,spring-boot,Spring,Spring Boot,我需要将会话存储外部化,所以使用了spring会话 按照他们在的示例,我创建了我的EmbeddedRedisConfiguration,一切都正常工作 我决定,对于预先存在的本地Redis服务器,我需要可选的支持来指定Redis可执行路径,因此我在/resources/config/application.properties中添加了以下键值Redis.embedded.executable.path==/path/to/Redis 我当时的想法是在配置中使用@Value注释,并访问该值 sta

我需要将会话存储外部化,所以使用了spring会话

按照他们在的示例,我创建了我的
EmbeddedRedisConfiguration
,一切都正常工作

我决定,对于预先存在的本地Redis服务器,我需要可选的支持来指定Redis可执行路径,因此我在
/resources/config/application.properties
中添加了以下键值
Redis.embedded.executable.path==/path/to/Redis

我当时的想法是在配置中使用@Value注释,并访问该值

static class RedisServerBean implements InitializingBean, DisposableBean, BeanDefinitionRegistryPostProcessor {
    private RedisServer redisServer;

    @Value("${redis.embedded.executable.path}")
    String executablePath;

    public void afterPropertiesSet() throws Exception {
        if (executablePath != null) {
            redisServer = new RedisServer(new File(executablePath), Protocol.DEFAULT_PORT);    
        } else {
            redisServer = new RedisServer(Protocol.DEFAULT_PORT);
        }
        redisServer.start();
    }

    public void destroy() throws Exception {
        if(redisServer != null) {
            redisServer.stop();
        }
    }

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {}

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
}
但是,
executablePath
始终为空。如您所知,如果在
@服务
类或等效类中使用
@值
,则会填充该值

我假设此配置在加载属性的bean之前被调用,但我也知道这是可能的,因为例如
@DatasourceAutoConfiguration
可以使用
spring.datasource.
属性


我显然忽略了一些简单的事情。我是否需要自己的
@ConfigurationProperties

将您的属性文件更改为:

redis.embedded.executable.path=/path/to/redis

你能给我们看一下你的配置文件吗?这个bean实际上是由Spring管理的吗?这个配置文件与上面提供的链接相同,添加了我的
@Value
注释字段。为什么需要
bean定义注册表后处理器?它导致了在绑定
@值之前的早期实例化。就是这样。我猜这就是复制粘贴所发生的情况,即使是从官方来源:)我的问题是,我已经将它添加到我的
应用程序中。properties
你有两个等号kabalt这是一个答案Rüdiger。kabal在属性文件中有一个错误,我正在修复它extra=只是配置值配置错误,而不是空值的原因。