java - 如何将前缀属性注入(inject)java.util.Properties?

标签 java spring spring-mvc spring-boot

Spring boot 提供了一种优雅的方式,使用 @ConfigurationProperties(prefix = "foo") 将带有特定键前缀的属性注入(inject)到 Configuration 类中。显示herehere 。问题是,如何将前缀属性注入(inject)到 java.util.Properties 实例中,如下所示?

@Configuration
@EnableConfigurationProperties
public class FactoryBeanAppConfig {

    @Bean
    @ConfigurationProperties(prefix = "kafka")
    public Producer<String, String> producer(Properties properties) throws Exception {
        Producer<String, String> producer = new KafkaProducer<String, String>(properties);
        return producer;
    }

}

最佳答案

这是行不通的,因为这个属性注入(inject)是基于应该保存@ConfigurationProperties的对象上的getter和setter 定义一个包含您想要的属性的类,如下所示:

@ConfigurationProperties(prefix = "kafka.producer")
public class MyKafkaProducerProperties {

  private int foo;

  private string bar;

  // Getters and Setter for foo and bar

}

然后在您的配置中使用它,如下所示

@Configuration
@EnableConfigurationProperties(MyKafkaProducerProperties.class)
public class FactoryBeanAppConfig {

  @Bean
  public Producer<String, String> producer(MyKafkaProducerProperties kafkaProperties) throws Exception {
    Properties properties = new Properties();
    properties.setProperty("Foo", kafkaProperties.getFoo());
    properties.setProperty("Bar", kafkaProperties.getBar());
    Producer<String, String> producer = new KafkaProducer<String, String>(properties);
    return producer;
  }
}

更新

既然您评论说您不希望将每个属性都表示为 java 代码,您可以使用 HashMap 作为 @ConfigurationProperties 中唯一的属性

@ConfigurationProperties(prefix = "kafka")
public class MyKafkaProducerProperties {

  private Map<String, String> producer= new HashMap<String, String>();

  public Map<String, String> getProducer() {
    return this.producer;
  }
}

application.properties 中,您可以指定如下属性:

kafka.producer.foo=hello
kafka.producer.bar=world

在您的配置中,您可以像这样使用它:

@Configuration
@EnableConfigurationProperties(MyKafkaProducerProperties.class)
public class FactoryBeanAppConfig {

  @Bean
  public Producer<String, String> producer(MyKafkaProducerProperties kafkaProperties) throws Exception {

    Properties properties = new Properties();

    for ( String key : kafkaProperties.getProducer().keySet() ) {
     properties.setProperty(key, kafkaProperties.getProducer().get(key));
    }

    Producer<String, String> producer = new KafkaProducer<String, String>(properties);
    return producer;
  }
}

关于java - 如何将前缀属性注入(inject)java.util.Properties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48417204/

相关文章:

java - 消息卡在 activemq 队列中

java - 无法在 Spring Mavenized 元素中加载 CSS

java - Spring:RestController 和 Controller 的不同异常处理程序

spring-boot - Spring Boot 2+ 无法 Autowiring 。 'UserDetailsService'的bean不止一个

spring-mvc - 是否可以使用 Spring MVC + PrimeFaces?

java - 当我启动 apk 文件时出现以下错误

java - hibernate 不创建表但没有错误消息

java - 通过使用 Lambda 函数匹配 map 列表和对象列表之间的字段来创建列表组合?

javascript - 未捕获的类型错误 : Cannot read property '0' of undefined - in jqGrid Spring MVC

spring-mvc - 如何使用 RedirectAttributes 测试 Spring MVC Controller