【问题标题】:Spring @RefreshScope is not working with @ComponentSpring @RefreshScope 不适用于 @Component
【发布时间】:2017-03-06 09:17:09
【问题描述】:

我有组件类和 pom.xml 依赖项,如下所示。属性永远不会被设置并保持为空。

@Component
@RefreshScope
public class SecurityProperties1  {

    @Value("${ad.url}")
    public String adUrl;

    @Value("${ad.manager.dn}")
    public String managerDN;

    @Value("${ad.manager.password}")
    public String managerPassword;

    @Value("${ad.search.base}")
    public String searchBase;

    @Value("${ad.user.filter}") 
    public String userFilter;

}

pom.xml

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>1.2.1.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-commons -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>1.1.4.RELEASE</version>
        </dependency>

另外, 我的财产来源如下

@Component
public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    @Override
    protected String resolvePlaceholder(String placeholder, Properties props) {
        return DynamicProperty.getProperty(placeholder);
    }

     @Override
     protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
         return DynamicProperty.getProperty(placeholder);
     }

}

【问题讨论】:

  • 对于初学者,请停止混合 Spring Cloud 版本。而您的自定义 PropertyPlaceholderConfigurer 会破坏正确使用。

标签: spring-boot spring-cloud spring-boot-actuator


【解决方案1】:

如果有人遇到此问题,则对于较新版本:
确保您在类路径中具有 spring-cloud-starter-bootstrap 依赖项
并在 bootstrap.properties 文件中添加 spring.application.name 属性
并使用 @RefreshScope 注释每个从配置服务器获取属性的类

【讨论】:

    【解决方案2】:

    我有同样的问题。我的解决办法:我加了proxymode = default注解

    @Component
    @RefreshScope(proxyMode = DEFAULT)
    public class MyClass {
    
       @Value("${test.value}")
       private String testValue;
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 2015-11-15
      • 2020-05-15
      • 2017-12-21
      • 2017-04-22
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多