1

我有一个 xmlConfiguration bean 来加载 SystemProperty.xml 文件,如下所示

<bean
    id="xmlConfiguration"
    class="org.apache.commons.configuration.XMLConfiguration"
    lazy-init="true">
    <constructor-arg type="java.lang.String">
        <value>SystemProperty.xml</value>
    </constructor-arg>
    <property name="expressionEngine">
        <bean class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" />
    </property>
</bean>

它工作正常,但是,我必须在 XMLConfiguration 中将 delimiterParsingDisabled 设置为 true,所以我更改 bean 以添加属性delimiterParsingDisabled

<bean
    id="xmlConfiguration"
    class="org.apache.commons.configuration.XMLConfiguration"
    lazy-init="true">
    <constructor-arg type="java.lang.String">
        <value>SystemProperty.xml</value>
    </constructor-arg>
    <property name="expressionEngine">
        <bean class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" />
    </property>
    <property name="delimiterParsingDisabled">
        <value type="java.lang.Boolean">true</value>
    </property>
</bean>

但是,这不会很好地工作。由于setDelimiterParsingDisabled()必须在加载文件之前调用。因此,我必须在调用load(String fileName)后调用 XMLConfigurationsetDelimiterParsingDisabled()

我用过MethodInvokingFactoryBean这种方式,但有任何异常如下

org.apache.commons.configuration.ConfigurationException: No file name has been set!
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:409)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.save(AbstractHierarchicalFileConfiguration.java:214)
at devicemanage.system.SystemConfigurationServiceImpl.saveSystemProperty(SystemConfigurationServiceImpl.java:232)
at datacollection.service.DataCollectionServiceImpl.syncWithDataCollection(DataCollectionServiceImpl.java:786)
at devicemanage.utility.SyncWithDCListener$1.run(SyncWithDCListener.java:51)

似乎该文件没有设置为 XMLConfiguration,我MethodInvokingFactoryBean的描述如下

<bean id="xmlConfigurationMethodInvokingBean"  
     class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
     <property name="targetObject" ref="xmlConfiguration" />  
     <property name="targetMethod" value="load" />
     <property name="arguments" value="SystemProperty.xml" />  
 </bean>

当然,将我的xmlConfigurationbean 更改为在新建构造函数时不加载文件,如下所示

<bean
    id="xmlConfiguration"
    class="org.apache.commons.configuration.XMLConfiguration"
    lazy-init="true">
    <property name="expressionEngine">
        <bean class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" />
    </property>
    <property name="delimiterParsingDisabled">
        <value type="java.lang.Boolean">true</value>
    </property>
</bean>

不确定是我使用错误的方法MethodInvokingFactoryBean还是我使用的参数错误将文件名字符串传递给load()

任何帮助表示赞赏。

4

1 回答 1

1

第一种方式

我建议您创建自己的继承类并声明init-method

package beans;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class CustomXMLConfiguration extends XMLConfiguration {

    private String loadFileName;

    private void init() throws ConfigurationException {
        this.load(fileName);
    }

    public String getLoadFileName() {
        return loadFileName;
    }

    public void setLoadFileName(String fileName) {
        this.loadFileName = fileName;
    }
}

在配置中,您可以通过以下方式使用此类:

<bean id="xmlConfiguration" class="beans.CustomXMLConfiguration" lazy-init="true"
                            init-method="init">
    <property name="expressionEngine">
        <bean class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" />
    </property>
    <property name="delimiterParsingDisabled">
        <value type="java.lang.Boolean">true</value>
    </property>
    <property name="loadFileName" value="SystemProperty.xml"/>
</bean>

init()方法将在 bean 初始化后立即调用。

第二种方式

您可以使用 beanMethodInvokingBean 。此类的 Bean 只是调用目标方法:

<bean class="org.springframework.beans.factory.config.MethodInvokingBean">
    <property name="targetObject" ref="xmlConfiguration"/>
    <property name="targetMethod" value="load"/>
    <property name="arguments" value="SystemProperty.xml"/>
</bean>

就我个人而言,我更喜欢第一个变体,因为更灵活的定制并且没有多余的 bean 实例。但是你可以选择任何人。

希望这会有所帮助。

于 2016-03-04T08:29:09.110 回答