我想为自定义资源前缀编写一个自己的Resource(来自core.io包)实现,例如: “myprotocol:/root/test/foo.properties”
最初的想法是引用JCR存储库中的Apache Sling资源路径来加载一些属性文件,然后PropertyPlaceholderConfigurer可以在Spring应用程序上下文中使用它,例如:
<context:property-placeholder properties-ref="appConfig" ignore-unresolvable="true" />
<bean id="appConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>jcr:/app/test/foo.properties</value>
</list>
</property>
</bean>
有人知道如何实现这个吗?
感谢您的帮助! 奥利
答案 0 :(得分:1)
资源路径的解析是在DefaultResourceLoader类的getResource(String)方法中以固定方式执行的,该方法是所有应用程序上下文的超类。
如何解决问题的一个想法是将应用程序上下文子类化。
public class CustomXmlApplicationContext extends AbstractXmlApplicationContext {
private final CustomResourceLocator customResourceLocator;
@Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
if (location.startsWith("custom:")) {
return customResourceLocator.getResource(location);
}
return super.getResource(location);
}
}
答案 1 :(得分:0)
自Spring 4.3 DefaultResourceLoader
现在有一个addProtocolResolver()
方法,允许您提供带有String的ProtocolResolver
接口的实现,并返回ResourceLoader
的实现。您可以加载由String值标识的资源。