Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @RefreshScope is not working with @Component

I've the component class and pom.xml dependencies like below. 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

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

}
like image 667
srinannapa Avatar asked Oct 24 '16 14:10

srinannapa


2 Answers

I had same problem. My solution: I added proxymode = default annotation

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

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

  }
like image 136
Aleks D Avatar answered Oct 29 '22 01:10

Aleks D


For newer version if somebody is facing this issue :
Make sure you have spring-cloud-starter-bootstrap dependency in classpath
and also add spring.application.name property in your bootstrap.properties file
And annotated each class that is getting property from config server with @RefreshScope

like image 42
Jay Yadav Avatar answered Oct 29 '22 03:10

Jay Yadav