- I am using spring-context 5.3.13 (no spring-boot)
- I have a file named "config.yaml" under "src/main/resources"
- I have this @PropertySource configured:
@PropertySource(value = "classpath:config.yaml", factory = TypesafeAdapterPropertySourceFactory.class)
- When I run it from intellij it successfully loads the file
My issue: When running a jar the file is not found due to the path being unavailable inside the jar. More info:
- the file itself is inside the jar root (checked it)
- Exception:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.viber.httpmsexample.Application]; nested exception is java.io.FileNotFoundException: class path resource [config.yaml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Repositories/http-ms-example/target/app.jar!/config.yaml at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:93) at com.mytest.httpmsexample.Application.main(Application.java:63) Caused by: java.io.FileNotFoundException: class path resource [config.yaml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Repositories/http-ms-example/target/app.jar!/config.yaml at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217) at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:162) at com.mytest.libraries.anticorruption.config.TypesafeAdapterPropertySourceFactory.createPropertySource(TypesafeAdapterPropertySourceFactory.java:14) at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:463) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:280) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:304) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175) ... 8 more
I can externalize this config but I wish to keep it there for now. Is it possible to get over that issue?
Thanks!
CodePudding user response:
The problem is your own TypesafeAdapterPropertySourceFactory
which tries to create the property source based on a File
which doesn't work when the file is in a jar. A File in the sense of java is a physical file on the filesystem, one that is inside an archive isn't.
I would strongly suggest to use the ResourcePropertySource
to create the property source and decorate that instead of something else.