使用MethodInvokingFactoryBean进行XMLConfiguration以在Spring bean中加载文件

时间:2016-03-04 07:24:05

标签: java spring spring-bean xml-configuration method-invocation

我有一个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)

之后调用XMLConfiguration的setDelimiterParsingDisabled()

我已经以这种方式使用了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>

原因是,将我的xmlConfiguration bean更改为不加载文件,而新的构造函数如下所示

<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的方法是否错误,或者我使用了将fileName字符串传递给load()的参数的错误

感谢任何帮助。

1 个答案:

答案 0 :(得分: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>
在初始化bean之后,将立即调用

init()方法。

第二种方式

您可以使用MethodInvokingBean class的bean。这个类的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实例。但你可以选择任何人。

希望这会有所帮助。