I have a very small Spring Boot program that attempts to add its own properties in the relevant class
package com.findology.testboot2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration
@PropertySource({ /*"classpath:/application.properties",*/ "classpath:/countrycounts.properties" })
@Component
public class CountryCountsWebSetup implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Value("${countrycounts.server.port}")
int port;
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(port);
}
}
and a properties file
countrycounts.kafka.bootstrapServers= kafka1.data.int.dc1.ad.net:9092,kafka2.data.int.dc1.ad.net:9092,kafka3.data.int.dc1.ad.net:9092,kafka4.data.int.dc1.ad.net:9092,kafka5.data.int.dc1.ad.net:9092
countrycounts.kafka.topic= COUNTRY_COUNTS_WINDOWS
countrycounts.kafka.listener.id= country_counts_window
countrycounts.server.port= 8989
The problem I'm having is that while running with debug turned on Spring Boot appears to find and load the countrycounts.properties
file
2018-08-29 17:14:10.697 [main; MutablePropertySources] DEBUG -- Adding PropertySource 'class path resource [countrycounts.properties]' with search precedence immediately higher than 'class path resource [f2-traffic.properties]'
but cannot complete startup because it claims the countrycounts.server.port
property is missing.
2018-08-29 17:14:12.705 [main; AbstractApplicationContext] WARN -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JettyServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedJetty.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryCountsWebSetup': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'countrycounts.server.port' in value "${countrycounts.server.port}"
If I set the value on the java command line using -D
, it works fine but still implies that the property was not read from the file.
Any ideas why the property file gets loaded but the values within are not used?
UPDATE
As part of the maven dependencies for this program, a large (and complicated) jar file is included which uses Spring 4.1 and includes a propertyPlaceHolder
and it also defines location
. Could that be destroying the path to the properties file I need thus causing those properties not to be seen?