簡體   English   中英

如何使用PropertyPlaceholderConfigurer使用自定義xml文件將變量加載到我的spring.xml中?

[英]How to use a custom xml file to load variables to my spring.xml using PropertyPlaceholderConfigurer?

我正在嘗試使用自定義xml作為屬性文件,以便可以在spring.xml文件中定義一些變量。

我搜索了類似的問題,但是xml文件必須遵循要使用的標准。

通常我會這樣加載.properties文件:

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

因此,我可以將屬性以${variable}的形式注入到spring.xml中。

我的自定義xml如下所示:

<configAttributes>
<attributes>
    <attribute>
        <name>NameOftheVariable</name>
        <description>Text</description>
        <value>valueIWantoToInject</value>
    </attribute>
<attributes>

我正在使用spring-beans 2.5和Java EE 6。

謝謝。

使用任何解析器讀取xml文件,我假設您已經按照注釋中的說明進行了操作。

因此,現在您在spring配置中有一個客戶端bean,它需要從xml解析值中注入屬性。

因此,只需在春季加載該豆作為

BeanClass bean = `context.getBean(clientBeanid);

然后將解析的值從xml文件設置為bean,如下所示

bean.setName(parsedxmlname);
bean.setName(parsedxmldescription);

等等。

這是一個兩步解決方案

  1. 覆蓋PropertyPlaceholderConfigurer您必須覆蓋resolvePlaceholder(),在該處將在邏輯中插入以返回預期結果

  2. 解析xml解析器應有助於將xml讀取為所需格式,並將其提供給您的resolvePlaceholder()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM