Spring @RefreshScope不能与@Component一起使用

问题描述 投票:4回答:1

我有像下面这样的组件类和pom.xml依赖项。永远不会设置属性并保持为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.hml

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

另外,My Property来源如下

@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-boot spring-cloud spring-boot-actuator
1个回答
2
投票

我有同样的问题。我的解决方案:我添加了proxymode = default注释

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

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

  }
© www.soinside.com 2019 - 2024. All rights reserved.