Environment中获取配置
- 方式一:直接getProperties获取
String bootstrapServers = env.getProperty("hello.kafka.bootstrap-servers");
- 方式二:将属性直接绑定到对象上
BindResult<TestKafkaProperties> kafkaProperties = Binder.get(env).bind("hello.kafka", TestKafkaProperties.class);
- 具体Properties类编写
@Data @ConfigurationProperties(prefix = "hello.kafka") public class TestKafkaProperties implements Serializable { private String bootstrapServers ; private List<TestProducerProperties> producers = new ArrayList<>(); @Data public static class TestProducerProperties implements Serializable { private String username ; private String password ; private Integer retries = 0; private Integer batchSize = 16384; private Integer linger = 1; } }
文章来源地址https://www.toymoban.com/news/detail-705700.html
文章来源:https://www.toymoban.com/news/detail-705700.html
到了这里,关于spring实战笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!