@ConfigurationProperties 前缀不起作用

新手上路,请多包涵

.yml 文件

cassandra:
    keyspaceApp:junit
solr:
    keyspaceApp:xyz

@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {
   @Value("${keyspaceApp:@null}") private String keyspaceApp;

主要方法文件

@EnableAutoConfiguration
@ComponentScan
@PropertySource("application.yml")
public class CommonDataApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(CommonDataApplication.class)
                .web(false).headless(true).main(CommonDataApplication.class).run(args);
    }
}

测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CommonDataApplication.class)
@IntegrationTest
@EnableConfigurationProperties
public class CassandraClientTest {

    @Autowired
    CassandraClientNew cassandraClientNew;

    @Test
    public void test(){
        cassandraClientNew.getSession();
        System.out.println(" **** done ****");
    }
}

它没有将 junit 设置为 keyspaceApp,而是设置了 xyz。

看起来 prefix=“cassandra” 不工作

原文由 plzdontkillme 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 768
2 个回答

看起来您正在尝试使用 Spring Boot Typesafe Configuration Properties 功能。

因此,为了让它正常工作,您必须对代码添加一些更改:

首先,你的 CommonDataApplication 类应该有 @EnableConfigurationProperties 注释例如

@EnableAutoConfiguration
@ComponentScan
@PropertySource("application.yml")
@EnableConfigurationProperties
public class CommonDataApplication {

    public static void main(String[] args) {
        // ...
    }
}

I do not believe you need @PropertySource("application.yml") annotation as application.yml (as well as application.properties and application.xml ) is a default configuration file used by Spring启动。

您的 CassandraClientNew 类不需要 @Value 注释前缀 keyspaceApp 属性。你的 keyspaceApp 必须有一个 setter 方法

 @Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {

   private String keyspaceApp;

   public void setKeyspaceApp(final String keyspaceApp) {
       this.keyspaceApp = keyspaceApp;
   }
}

顺便说一句,如果您正在使用 ListSet s 并且您初始化集合(例如 List<String> values = new ArrayList<>(); ),则只需要 get-ter 33e如果一个集合没有被初始化,那么你也需要提供一个 setter 方法(否则会抛出异常)。

我希望这会有所帮助。

原文由 Tom 发布,翻译遵循 CC BY-SA 4.0 许可协议

一般回答

1. 在你的属性文件中(application.properties 或 application.yml)

 # In application.yaml
a:
  b:
    c: some_string

2. 声明你的班级:

 @Component
@ConfigurationProperties(prefix = "a", ignoreUnknownFiels = false)
public class MyClassA {

  public MyClassB theB;   // This name actually does not mean anything
                          // It can be anything
  public void setTheB(MyClassB theB) {
    this.theB = theB;
  }

  public static MyClassB {

    public String theC;

    public void setTheC(String theC) {
      this.theC = theC;
    }

  }

}

3. 声明公共设置器!这是至关重要的!

确保在上述类中声明了这些公共方法。确保他们有 “公共” 修饰符。

 // In MyClassA
public void setTheB(MyClassB theB) {
  this.theB = theB;
}

// In MyClassB
public void setTheC(String theC) {
  this.theC = theC;
}

而已。

最后的笔记

  • 类中的属性名称对 Spring 没有任何意义。它只使用公共二传手。我宣布他们 public 不要在这里宣布 public getters。您的属性可能有任何访问修饰符。
  • 注意属性“ignoreUnknownFields”。它的默认值为“真”。当它为“false”时,如果文件“application.yml”中的任何属性未绑定到任何类属性,它将抛出异常。这将在调试过程中为您提供很多帮助。

原文由 armansimonyan13 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题