简体   繁体   English

Spring @RefreshScope 不适用于 @Component

[英]Spring @RefreshScope is not working with @Component

I've the component class and pom.xml dependencies like below.我有组件类和 pom.xml 依赖项,如下所示。 The properties are never set and staying as null.这些属性永远不会设置并保持为空。

@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 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>

Also, My Property source is like below此外,我的财产来源如下

@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);
     }

}

I had same problem. 我有同样的问题。 My solution: I added proxymode = default annotation 我的解决方案:我添加了proxymode = default annotation

@Component
@RefreshScope(proxyMode = DEFAULT)
public class MyClass {

   @Value("${test.value}")
   private String testValue;

  }

For newer version if somebody is facing this issue :对于较新版本,如果有人遇到此问题:
Make sure you have spring-cloud-starter-bootstrap dependency in classpath确保你在类路径中有 spring-cloud-starter-bootstrap 依赖
and also add spring.application.name property in your bootstrap.properties file并在 bootstrap.properties 文件中添加 spring.application.name 属性
And annotated each class that is getting property from config server with @RefreshScope并使用@RefreshScope 注释从配置服务器获取属性的每个类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM