我正在尝试编写一个可以动态加载插件的框架。这个加载过程的一个步骤是加载属性文件“properties.plugin”我想将它加载到PropertyPlaceholderConfigurer中,但是看起来它使用的是与GenericApplicationContext设置使用的类加载器不同的类加载器。
我创建上下文的代码:
GenericApplicationContext ctx = new GenericApplicationContext();
if(classLoader !=null)
ctx.setClassLoader(classLoader);
ctx.getDefaultListableBeanFactory().setAllowBeanDefinitionOverriding(false);
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.setBeanClassLoader(classLoader);
int totalBeanCount = 0;
List<Resource> processedResources = new ArrayList<Resource>(resources.length);
for(Resource r:resources)
{
try
{
int loadedBeanCount = xmlReader.loadBeanDefinitions(r);
totalBeanCount += loadedBeanCount;
processedResources.add(r);
}
catch(BeanDefinitionStoreException e)
{
throw e;
}
}
上面代码中的classLoader在启动时设置。
bean的设置如下:
<bean name="ReloadedPropertiesPlaceholderConfigurer" class="myProject.MyPropertyPlaceholderConfigurer" >
<property name="locationNames" ref="locationNamesList" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean name="locationNamesList" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>properties/properties.plugin</value>
</list>
</constructor-arg>
</bean>
myProject.MyPropertyPlaceholderConfigurer只是扩展了PropertyPlaceholderConfigurer
当我稍后在代码中调用ctx.refresh()时,我看到一条日志消息,说找不到属性/ properties.plugin
以下代码
classLoader.getResource("properties/properties.plugin");
如果我在上下文中设置类加载器之前添加它就可以工作。
我也尝试过classpath:properties / properties.plugin,properties.plugin,classpath *:properties.plugin。
这个PropertyPlaceholderConfigurer使用的是不同于ctx上设置的类加载器的情况吗?如果有的话,是否有办法设置它使用的类加载器?
我没有看到setClassLoader方法。
答案 0 :(得分:0)
Thread.currentThread().setContextClassLoader(classLoader);
似乎使它工作,所以它必须使用这个类加载器而不是上下文中的一个。