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

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

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

  1. @RefreshScope
  2. @Component
  3. @Configuration
  4. @EnableConfigurationProperties
  5. @ConfigurationProperties
  6. @Getter
  7. public class PropConig{
  8. @Value("${welcome.message}")
  9. private String message;
  10. }

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

  1. @EnableJms
  2. @Log4j2
  3. public class JmsConfig{
  4. @AutoWired PropConig propConig;
  5. -Here I am accessing the values using**propConig.message**
  6. }

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

mspsb9vt

mspsb9vt1#

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

  1. @Configuration
  2. @ConfigurationProperties(prefix = "sample")
  3. public class ConfigProperties {
  4. private String hostName;
  5. private int port;
  6. private String from;
  7. // standard getters and setters
  8. }

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

相关问题