spring实战笔记

Environment中获取配置

  1. 方式一:直接getProperties获取
    String bootstrapServers = env.getProperty("hello.kafka.bootstrap-servers");
    
  2. 方式二:将属性直接绑定到对象上
    BindResult kafkaProperties = Binder.get(env).bind("hello.kafka", TestKafkaProperties.class);
    
  3. 具体Properties类编写
    @Data
    @ConfigurationProperties(prefix = "ldk.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;
    	}
    }
    

ate Integer linger = 1;
}
}
```

你可能感兴趣的:(spring,笔记,java)