2014-04-16 22 views
0

我在下面的spring配置中使用來自java Classess的文件中的訪問權限。 有對JavaEE的項目很多方面類似如下:如何在Java SE項目中使用PropertyPlaceholderConfigurer

<context:component-scan base-package="com.service.pack" /> 
    <context:property-placeholder properties-ref="appProperties"/> 

我可以使用上面的Java SE項目,以試圖PropertyPlaceholderConfigurer類的方法,但在這個時候applicationProperties豆不是從PropertyPlaceholderConfigurer類見過這麼propertiesMap爲空。

我在做什麼錯?有沒有從類文件一樣的JavaEE accecing屬性的簡單執行的項目

<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="classpath:app.properties"/> 
</bean> 



@Component 
public class PropertiesUtil extends PropertyPlaceholderConfigurer { 
    private static Map<String, String> propertiesMap; 

    @Override 
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, 
      Properties props) throws BeansException { 
     super.processProperties(beanFactory, props); 

     propertiesMap = new HashMap<String, String>(); 
     for (Object key : props.keySet()) { 
      String keyStr = key.toString(); 
      propertiesMap.put(keyStr, parseStringValue(props.getProperty(keyStr), 
       props, new HashSet())); 
     } 
    } 

    public static String getProperty(String name) { 
     return (String) propertiesMap.get(name); 
    } 
} 
+0

爲什麼它會在Jave SE項目中有所不同......有沒有區別... –

+0

當processProperties(方法被調用,Properties props參數爲null時,爲什麼? –

回答

0

我不知道你想做什麼,但在你上面的示例項目,PropertiesUtilPropertyPlaceholderConfigurer延伸,但XML配置是指PropertyPlaceholderConfigurer而不是你的子類。所以,如果這就是你的全部,那麼你的課堂甚至不會被調用。

我想,在本地用下面的配置和它的工作:

<bean id="applicationProperties" class="com.foo.yourpackage.PropertiesUtil"> 
    <property name="location" value="classpath:app.properties"/> 
</bean> 

現在,我明白你想訪問你的code.The Environment抽象的性質已經所有你需要,檢查this blog postjavadoc of Environment瞭解更多詳情。

相關問題