Situation
I have a fat .jar of a Spring boot application. I've externalized my configuration with an application.properties
file. This file is in the same folder as the .jar, and I'm starting the .jar from the command line from within the same folder (with the command "java -jar $jarFileName").
Then an exception is thrown:
nested exception is org.springframework.beans.TypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is
java.lang.NumberFormatException: For input string: "${elasticsearch.port}"
As you can see, instead of reading the value from the properties file, it just sets the string as the text in the @Value annotation, which looks like this:
@Value("${elasticsearch.port}")
private int elkPort;
The class this happens in is annotated with @Component
.
According to Spring docs: externalized configuration, spring should read an application.properties
file outside of the jar.
When the same application.properties
file is placed in src/main/resources
it works fine, so the configuration file seems correct.
Any ideas why it won't load the external configuration file?
EDIT 1
I've also tried running it with --spring.config.location=file:application.properties
and --spring.config.location=file:/full/path/to/application.properties
but with the same result as above.
EDIT 2: classpath attempt
Also tried classpath
instead of file
, the same as the commands above but file
replaced with classpath
.
Lastly tried without either, so just --spring.config.location=/path/to/file
; again both with relative and full path to the application.properties
. All attempts gave the same result/exception.
EDIT 3 My annotated application:
@SpringBootApplication
public class ApplicationName {
public static void main(String[] args) {
SpringApplication.run(ApplicationName.class, args);
}
}
EDIT 4
Tried adding a PropertySourcesPlaceholderConfigurer
as follows:
@Configuration
public class PropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
And then for each @Value
I added a default value; it still only resolves to the default values instead of to the application.properties
values.