javaspringspring-bootspring-mvcspring-cloud-config-server

Appication error whlie using @Refreshscope


I am having a Config server and Config Client, I have injected property file value to my java class as shown below,

@RefreshScope
@Component
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
@Getter
public class PropConig{
      @Value("${welcome.message}") 
      private String message;
}

Property file has containing below code

welcome.message = "Welcome to my app"

And I have a JMS configuration class @AutoWired with PropConig as below where I used above property value

@EnableJms
@Log4j2
public class JmsConfig{
    @AutoWired PropConig propConig;
     
    -Here I am accessing the values using **propConig.message** 

}

Everything working fine but when I am using @RefreshScope in PropConig.class it trowing error while starting the application. Kindly help me.


Solution

  • you can use configuration properties and remove @value,@refreshScope

    
    @Configuration
    @ConfigurationProperties(prefix = "sample")
    public class ConfigProperties {
        
        private String hostName;
        private int port;
        private String from;
    
        // standard getters and setters
    }
    

    value of hostName is value of sample.hostName or sample.host-name in your application.properties

    reference doc: https://www.baeldung.com/configuration-properties-in-spring-boot