从命名的 yaml 属性列表中读取字符串数组作为常量?
Read string array from named yaml properties list as a constant?
是否可以使用 Spring Java 从 YAML 文件的命名值映射中读取字符串数组作为常量(专门用于 @KafkaListener
注释来自 Spring-Kafka)?
a:
b:
topics:
foo: FooTopic
bar: BarTopic
会导致常量 = ['FooTopic','BarTopic'].
我假设它类似于计算结果为列表的表达式,就像 [Reading a List from properties file and load with spring annotation @Value ]. I'm just unsure of what the syntax might be, as the only way I've seen is using multiple annotations to construct a POJO like [ ] 中的某些示例一样,它在 @KafkaListener
注释上下文中不起作用.
注意:主题需要命名,因为它们是在不同上下文中单独提取的。
编辑:拼写
如果不提取 @ConfigurationProperties
,它不会那样工作。
这是我的额头:
@KafkaListener(topics = {"${kafka.topics.foo}", "${kafka.topics.bar}"})
但同时有这个:
@ConfigurationProperties("kafka")
public class KafkaAppProperties {
private Map<String, String> topics = new HashMap<>();
public Map<String, String> getTopics() {
return this.topics;
}
}
我能做到:
@KafkaListener(topics = "#{@'kafka-org.springframework.integration.samples.kafka.KafkaAppProperties'.topics.values()}")
是的,KafkaAppProperties
的 bean 名称看起来很尴尬,但我们可以用人造 bean 来克服它:
@Autowired
private KafkaAppProperties properties;
@Bean
public Map<String, String> kafkaTopic() {
return this.properties.getTopics();
}
...
@KafkaListener(topics = "#{@kafkaTopic.values()}")
public void handle(Object record) {
...
}
是否可以使用 Spring Java 从 YAML 文件的命名值映射中读取字符串数组作为常量(专门用于 @KafkaListener
注释来自 Spring-Kafka)?
a:
b:
topics:
foo: FooTopic
bar: BarTopic
会导致常量 = ['FooTopic','BarTopic'].
我假设它类似于计算结果为列表的表达式,就像 [Reading a List from properties file and load with spring annotation @Value ]. I'm just unsure of what the syntax might be, as the only way I've seen is using multiple annotations to construct a POJO like [ @KafkaListener
注释上下文中不起作用.
注意:主题需要命名,因为它们是在不同上下文中单独提取的。
编辑:拼写
如果不提取 @ConfigurationProperties
,它不会那样工作。
这是我的额头:
@KafkaListener(topics = {"${kafka.topics.foo}", "${kafka.topics.bar}"})
但同时有这个:
@ConfigurationProperties("kafka")
public class KafkaAppProperties {
private Map<String, String> topics = new HashMap<>();
public Map<String, String> getTopics() {
return this.topics;
}
}
我能做到:
@KafkaListener(topics = "#{@'kafka-org.springframework.integration.samples.kafka.KafkaAppProperties'.topics.values()}")
是的,KafkaAppProperties
的 bean 名称看起来很尴尬,但我们可以用人造 bean 来克服它:
@Autowired
private KafkaAppProperties properties;
@Bean
public Map<String, String> kafkaTopic() {
return this.properties.getTopics();
}
...
@KafkaListener(topics = "#{@kafkaTopic.values()}")
public void handle(Object record) {
...
}