简体   繁体   中英

Spring Boot doesn't read the default value from the application.properties file

I have this propelr.properties:

elasticsearch.port=443
elasticsearch.protocol=https
elasticsearch.index=contentlink
elasticsearch.type=cl
kafka.max-poll-records=1000
kafka.topic=es-indexer-topic
kafka.group.id=espn-content-link-kafka-es-connector-group
kafka.concurrency-level=4

And this file where I get the port:

@Component
final class ElasticSearchClientFactory extends 
AbstractFactoryBean<RestHighLevelClient> {

    private final RestHighLevelClient client;

    ElasticSearchClientFactory(
            final @Value("${elasticsearch.hostname}") String hostname,
            final @Value("${elasticsearch.port}") int port,
            final @Value("${elasticsearch.protocol}") String protocol
    ) {
        client = new RestHighLevelClient(RestClient.builder(new 
 HttpHost(hostname, port, protocol)));
    }

    @Override
    public Class<?> getObjectType() {
        return RestHighLevelClient.class;
    }

    @Override
    protected RestHighLevelClient createInstance() throws Exception {
        return client;
    }

    @Override
    public void destroy() throws Exception {
        if (Objects.nonNull(client)) {
            client.close();
        }
    }
}

And this portion of code is where the @Configuration is:

@EnableKafka
@Configuration
public class KafkaReceiverConfig {

@Value("${contentlink.kafka.bootstrap.servers}")
private String bootstrapServers;
@Value("${kafka.group.id}")
private String kafkaGroupId;
@Value("${kafka.max-poll-records}")
private String kafkaMaxPollRecords;
@Value("${kafka.concurrency-level}")
private int kafkaConcurrencyLevel;

private EspnMetricRegistry metricRegistry;

@Bean
public KafkaReceiver receiver(
        @Value("${elasticsearch.index}") final String index,
        @Value("${elasticsearch.type}") final String type,
        final RestHighLevelClient client, final EspnMetricRegistry metricRegistry) {
    return new KafkaReceiver(index, type, metricRegistry, client);
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, ESPNEntity>  kafkaListenerContainerFactory() {
  ConcurrentKafkaListenerContainerFactory<String, ESPNEntity> factory =
      new ConcurrentKafkaListenerContainerFactory<>();
  factory.setConsumerFactory(kafkaConsumerFactory());
  factory.setBatchListener(true);
  factory.setConcurrency(kafkaConcurrencyLevel);
  return factory;
}

@Bean
public ConsumerFactory<String, ESPNEntity> kafkaConsumerFactory() {
    final Deserializer<String> key = new StringDeserializer();
    final Deserializer<ESPNEntity> value = new ESPNEntitySerde(new ESPNEntityConverter());
    return new DefaultKafkaConsumerFactory<>(consumerConfigs(), key, value);
}

@Bean
public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ESPNEntitySerde.class);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaGroupId);
    props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, kafkaMaxPollRecords);
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    return props;
}

}

So, I don't know what could be causing this error or what I'm missing but when I deploy to aws, the stack gets stuck because when it reaches the part of the springboot, it fails and it throws these errors:

2018-12-19 16:28:45.381 ERROR 43 --- [ main] osboot.SpringApplication : Application startup failed

java.lang.IllegalArgumentException: Could not resolve placeholder 'elasticsearch.port' in value "${elasticsearch.port}"

You need to register your properties file. Add this line under @Component annotation:

@PropertySource("classpath:propelr.properties")

So it should looks like this:

@Component
@PropertySource("classpath:propelr.properties")
final class ElasticSearchClientFactory extends 
AbstractFactoryBean<RestHighLevelClient> {

private final RestHighLevelClient client;

ElasticSearchClientFactory(
        final @Value("${elasticsearch.hostname}") String hostname,
        final @Value("${elasticsearch.port}") int port,
        final @Value("${elasticsearch.protocol}") String protocol
) {
    client = new RestHighLevelClient(RestClient.builder(new 
 HttpHost(hostname, port, protocol)));
}

@Override
public Class<?> getObjectType() {
    return RestHighLevelClient.class;
}

@Override
protected RestHighLevelClient createInstance() throws Exception {
    return client;
}

@Override
public void destroy() throws Exception {
    if (Objects.nonNull(client)) {
        client.close();
    }
}
}

EDIT Static property source placeholder

@Bean   
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {      
    return new PropertySourcesPlaceholderConfigurer(); 
}

正如@Rafal所指,您应该指定@propertySource(“ classpath:application.properties”),以提供正确的路径以及要读取其属性的文件名。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM