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.
As it says on mentioned page, you should specify external config location
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Try without file keyword --spring.config.location=/full/path/application.properties
I just took my application.properties out of an Eclipse Spring Boot project and it failed. Then I put the file in a cfg folder in the root of the project and added program argument:
--spring.config.location=cfg/application.properties
and it worked again. Mayby if you try a relative path (no leading /) to the file (without the "file:") it will work.
Alright after quite some struggles, I've found the solution. I was close with PropertySourcesPlaceholderConfigurer
but not quite there yet; this is the full class now:
@Configuration
public class PropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List<Resource> resources = new ArrayList<>();
resources.add(new FileSystemResource("relative/path/to/application.properties"));
ppc.setLocations(resources.toArray(new Resource[]{}));
return ppc;
}
}
EDIT
To demonstrate the issue, I've created a repository to show the problem, see here: https://github.com/Locitao/test-external-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.