-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check exception cause for @PropertySource(ignoreResourceNotFound)
support
#22276
Comments
Any news on this? |
Hitting this issue as well. Any updates? |
In case it helps anyone, I am working around this in two parts:
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
factory.setResolutionMethod(YamlProcessor.ResolutionMethod.OVERRIDE_AND_IGNORE);
Properties properties = factory.getObject();
return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
}
|
My workaround is to catch the exception and rethrow its cause if it fits. So from now on, I will be living in fear of an update breaking this behavior. try {
final YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource);
factory.afterPropertiesSet();
return factory.getObject();
} catch (final java.lang.IllegalStateException illegalState) {
if (illegalState.getCause() instanceof final FileNotFoundException e) {
// ConfigurationClassParser wants FileNotFoundException to honor PropertySource.ignoreResourceNotFound
throw e;
} else {
throw illegalState;
}
} |
@PropertySource(ignoreResourceNotFound=true)
fails with PropertySourceFactory
which uses YamlPropertySourceLoader
@PropertySource(ignoreResourceNotFound=true)
fails with PropertySourceFactory
which uses YamlPropertySourceLoader
ignoreResourceNotFound
flag in @PropertySource
is ignored if PropertySourceFactory
throws IllegalStateException
As of Spring Framework 6.0, the issue is caused by the behavior of
Whereas, As mentioned by @crazyk2, one option would be to add In light of that, I am considering modifying |
Instead of switching to an |
@urld, I think that's a fine workaround in the interim. |
ignoreResourceNotFound
flag in @PropertySource
is ignored if PropertySourceFactory
throws IllegalStateException
ignoreResourceNotFound
flag in @PropertySource
if PropertySourceFactory
throws IllegalStateException
ignoreResourceNotFound
flag in @PropertySource
if PropertySourceFactory
throws IllegalStateException
@PropertySource(ignoreResourceNotFound)
support
This has been addressed in 4a81814 for inclusion in Spring Framework 6.0.12. Feel free to try it out with upcoming 6.0.x snapshots, and let us know if you run into any issues. |
For load properties from
.yaml
I useYamlPropertySourceFactory
.Config Example
YamlPropertySourceLoader
from spring-boot-2.1.1.release leads to process function which throw IllegalStateException (in my case because FileNotFound ) but catch doesn't catch such type of exception and ignoreResourceNotFound is unreachable.Here are two options:
A - add IllegalStateException to catch block
B - change type of exception from IllegalStateException to IllegalArgumentException.
I prefer option A.
The text was updated successfully, but these errors were encountered: