Java 使用MethodInvokingFactoryBean for XMLConfiguration在Springbean中加载文件
我有一个xmlConfiguration bean来加载SystemProperty.xml文件,如下所示Java 使用MethodInvokingFactoryBean for XMLConfiguration在Springbean中加载文件,java,spring,spring-bean,xml-configuration,method-invocation,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>S
<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>
但是,这样做不好。由于在加载文件之前必须调用setDelimiterParsingDisabled()
。因此,在调用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>
不确定的是我使用MethodInvokingFactoryBean
的方法是否错误,或者我在将文件名字符串传递到load()
感谢您的帮助。第一条路 我建议您创建自己的继承类并声明
init方法
:
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实例。但你可以选择任何人
希望这会有帮助
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 class="org.springframework.beans.factory.config.MethodInvokingBean">
<property name="targetObject" ref="xmlConfiguration"/>
<property name="targetMethod" value="load"/>
<property name="arguments" value="SystemProperty.xml"/>
</bean>