Java 在spring中加载xml文件
我想将xml文件加载到Springbean中进行进一步处理。我知道,只要在spring配置文件中定义一个bean属性,就可以加载Java 在spring中加载xml文件,java,xml,spring,Java,Xml,Spring,我想将xml文件加载到Springbean中进行进一步处理。我知道,只要在spring配置文件中定义一个bean属性,就可以加载属性文件。可以对xml文件执行同样的操作吗 编辑 例如,我想要一个bean: public class Bean { private File file public void setFile(File file) { this.file = file } //.... } 在我的配置文件中,我想如下设置: <bean id="
属性文件。可以对xml文件执行同样的操作吗
编辑
例如,我想要一个bean:
public class Bean {
private File file
public void setFile(File file) {
this.file = file
}
//....
}
在我的配置文件中,我想如下设置:
<bean id="bean" class="blabla.Bean">
<property name="file" value="smth here"/>
</bean>
然后我想使用DOM解析xml,PropertyPlaceHolderConfigure已经通过
属性的xml文件格式如下所示
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="key1">Value 1</entry>
<entry key="key2">Value 2</entry>
</properties>
值1
价值2
你可以用
<context:property-placeholder
location="classpath:/com/myProject/spring_prop.xml" />
<bean id="bean" class="org.MyBean">
<property name="key1" value="${key1}" />
</bean>
将配置文件更改为:
<bean id="bean" class="blabla.Bean">
<property name="file" ref="file"/>
</bean>
<bean id="file" class="java.io.File">
<constructor-arg type="string"><value>C:\myfile.xml</value></constructor-arg>
</bean>
C:\myfile.xml
您可以通过将参数类型更改为资源来实现它-更多详细信息,请参阅。您可以从资源对象获取文件句柄
public class Bean {
private File file
public void setFile(Resource resource) {
this.file = resource.getFile()
}
//....
}
在配置文件中,您可以这样设置它。如果文件是类路径中jar的一部分,则可以使用classpath。如果在外部,则需要使用该文件:
<bean id="bean" class="blabla.Bean">
<property name="file" value="file:path to file"/>
</bean>
的可能重复,我不确定我是否理解这个问题——您是在谈论属性,还是希望在Springbean中加载XML文件,只想在配置中定义哪个文件?