springboot 动态给注解属性参数赋值

这边是结合最近开发的kafka功能加以示例

yml配置文件

1 spring:
2   kafka:
3     custom:
4       topic: xxx
5       groupId: oooo

配置映射的Java bean

 1 import lombok.Data;
 2 import org.springframework.boot.context.properties.ConfigurationProperties;
 3 import org.springframework.stereotype.Component;
 4  
 5 /**
 6 * @Author: shaww
 7 * @Date:
 8 */
 9 @Component
10 @ConfigurationProperties(prefix = "spring.kafka.custom")
11 @Data
12 public class KafkaCustomProperties {
13     private String topic;
14     private String groupId;
15 }

Controller里使用配置属性进行注解动态设值

1 @Autowired
2 private KafkaCustomProperties kafkaCustomProperties;
3  
4 @KafkaListener(topics = {"#{@kafkaCustomProperties.topic}"}, groupId = "#{@kafkaCustomProperties.groupId}")
5 public void listen(String message) {
6 }

 

posted @ 2021-12-28 10:28  夏威夷8080  阅读(2832)  评论(0)    收藏  举报