2012-02-27 92 views
0

我想写一个框架,其中插件可以动态加载。在这个加载过程中的一个步骤是加载一个属性文件“properties.plugin”我想加载到一个PropertyPlaceholderConfigurer,但它似乎使用了一个不同的Classloader,而不是GenericApplicationContext被设置为使用的那个。PropertyPlaceholderConfigurer与自定义类加载器

我为创建上下文代码:

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; 
     } 
    } 

类加载器在上面的代码被设置在启动时。

这个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
Thread.currentThread().setContextClassLoader(classLoader); 

似乎使它工作,所以它必须使用这个类加载器,而不是在上下文中设置一个。

相关问题