-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Gaurav Rawat opened SPR-13905 and commented
USE CASE
Hi I want to use @PropertySource in spring for a custom resource ie say xyz://path/x.txt instead of standard protocols like classpath file and so on which it already supports .I have found a way to do the same in spring Boot that is by SpringApplication as below with a custom ResourceLoader
protected WebApplicationContext run(SpringApplication app){
app.setResourceLoader(new MyCustomResourceLoader);
return (WebApplicationContext )app.run();
}The same works well and now I can define propertySource like @PropertySource("xyz://path/y.txt") .But I was not able to figure out how to override/customize the same in a normal spring application .
Stuff I will do in my custom ResourceLoader
public Class CustomResourceLoader extends DefaultResourceLoader{
@Override
public Resource getResource(String location){
if(location.startsWith("xyz://"){
return new XYZResource(new URI(location));
}
return super.getResource(location);
}Now I just need a way to tell spring to use this instead of the defaultresourceloader it's possible as I mentioned in SPring boot but need to figure out how to do it in pure Spring.
UPDATE
With the help of Chris Beams was able to figure out a way to do the same in CML apps via AnnotationConfigApplicationContext but still its not possible to do it conveninently for web apps unless you override AnnotationConfigWebApplicationContext getResourceXX methods and use your custom resource loader there (doesnt sound elegant) Hence I think we should either introduce ability to override resource loader via AnnotationConfigWebApplicationContext or come with something more elegant like we can do with Spring BOOT .
Affects: 4.1.7
Reference URL: http://stackoverflow.com/questions/35066913/using-custom-resource-protocol-in-propertysource-in-spring-using-custom-resourc
Issue Links:
- Add support to enhance default resource loader [SPR-12857] #17455 Add support to enhance default resource loader