【发布时间】:2011-08-30 16:16:26
【问题描述】:
我有一个由 Spring 创建的 bean。实际的类驻留在与 Spring 不同的 JAR 中。这个 bean 作为构造函数参数传递了一个路径。但是,我很难检索文件的句柄。该文件位于 WEB-INF/classes/ 中。我尝试过基于 WEB-INF 的相对路径,但显然没有奏效。
XML:
<bean id="configurationManager" class="package.ConfigurationManager"
scope="singleton">
<property name="configurationMapping">
<bean class="package.PropertiesFileConfigurationMapper">
<constructor-arg type="java.lang.String">
<value>/path/to/file</value>
</constructor-arg>
</bean>
</property>
</bean>
豆子:
public class ConfigurationMapper {
public ConfigurationMapper(String resource) {
_map = new HashMap<String, String>();
String property = null;
BufferedReader reader = null;
try {
FileReader file = new FileReader(resourcePath);
reader = new BufferedReader(file);
while ((property = reader.readLine()) != null) {
if (property.matches("(.+)=(.+)")) {
String[] temp = property.split("(.+)=(.+)");
_map.put(temp[0], temp[1]);
}
}
} catch (Exception ex){
ex.printStackTrace();
} finally {
if (reader != null)
reader.close();
}
}
//other methods to manipulate settings
}
如何获得rm.properties 文件的正确路径并在运行时将其传递给bean?
编辑:添加了构造函数代码。
编辑:我明白了。我将构造函数参数更改为不再采用路径。它现在需要一个资源,所以 Spring 找到了我想要加载的资源。
【问题讨论】:
-
试试类路径后缀。我的意思是类路径:filpath。