[英]Spring @RefreshScope is not working with @Component
我有組件類和 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);
}
}
我有同樣的問題。 我的解決方案:我添加了proxymode = default
annotation
@Component
@RefreshScope(proxyMode = DEFAULT)
public class MyClass {
@Value("${test.value}")
private String testValue;
}
對於較新版本,如果有人遇到此問題:
確保你在類路徑中有 spring-cloud-starter-bootstrap 依賴
並在 bootstrap.properties 文件中添加 spring.application.name 屬性
並使用@RefreshScope 注釋從配置服務器獲取屬性的每個類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.