2012-02-27 61 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); 

似乎使它工作,所以它必須使用這個類加載器,而不是在上下文中設置一個。

相關問題