使用@Refreshscope时发生应用程序错误

zf2sa74q  于 2022-10-30  发布在  Spring
关注(0)|答案(1)|浏览(179)

我有一个配置服务器和配置客户端,我已经将属性文件值注入到我的java类中,如下所示,

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

属性文件包含以下代码
welcome.message = "Welcome to my app"
我有一个JMS配置类@AutoWired,其中PropConig如下所示,我使用了上述属性值

@EnableJms
@Log4j2
public class JmsConfig{
    @AutoWired PropConig propConig;

    -Here I am accessing the values using**propConig.message**

}

一切正常,但当我在PropConig.class中使用@RefreshScope时,在启动应用程序时出现错误。请帮助我。

mspsb9vt

mspsb9vt1#

您可以使用配置属性并删除@value、@refreshScope

@Configuration
@ConfigurationProperties(prefix = "sample")
public class ConfigProperties {

    private String hostName;
    private int port;
    private String from;

    // standard getters and setters
}

hostName的值是www.example.com中的sample.hostName或sample.host-name的值application.properties
参考文档:https://www.baeldung.com/configuration-properties-in-spring-boot

相关问题