从 Spring 中的文件路径加载属性文件
Loading a properties file from a file path in Spring
我的应用程序上下文中有以下 bean:
<bean id="httpClient" factory-method="createHttpClient" class="com.http.httpclient.HttpClientFactory">
<constructor-arg>
<bean id="httpConfig" class="com.http.httpclient.HttpClientParamsConfigurationImpl">
<constructor-arg value="httpclient.properties"/>
</bean>
</constructor-arg>
</bean>
其中 httpclient.properties
是我的属性文件的名称。我在 HttpClientParamsConfigurationImpl
中使用此参数来读取文件(不要介意错误处理太多):
public HttpClientParamsConfigurationImpl(String fileName) {
try(InputStream inputStream = new FileInputStream("resource/src/main/properties/" + fileName)) {
properties.load(inputStream);
} catch (IOException e) {
LOG.error("Could not find properties file");
e.printStackTrace();
}
}
有没有办法在 bean 中传递整个文件位置,这样我就不必在创建 InputStream
时添加路径 resource/src/main/properties
?
我试过 classpath:httpclient.properties
但没用。
你的代码是错误的,文件在类路径中(src/main/resources
被添加到类路径中,其中的文件被复制到类路径的根目录中。在你的例子中,在一个名为 properties
).我建议您使用 Resource
或 Properties
而不是 String
。
public HttpClientParamsConfigurationImpl(Resource res) {
try(InputStream inputStream = res.getInputStream()) {
properties.load(inputStream);
} catch (IOException e) {
LOG.error("Could not find properties file");
e.printStackTrace();
}
}
然后在你的配置中你可以简单地写下:
<bean id="httpClient" factory-method="createHttpClient" class="com.http.httpclient.HttpClientFactory">
<constructor-arg>
<bean id="httpConfig" class="com.http.httpclient.HttpClientParamsConfigurationImpl">
<constructor-arg value="classpath:properties/httpclient.properties"/>
</bean>
</constructor-arg>
</bean>
或者甚至更好,甚至不用加载属性,只需将它们传递给构造函数,让 Spring 为您完成所有硬加载。
public HttpClientParamsConfigurationImpl(final Properties properties) {
this.properties=properties
}
然后使用 util:properties
加载属性并简单地为构造函数引用它。
<util:properties id="httpProps" location="classpath:properties/httpclient.properties" />
<bean id="httpClient" factory-method="createHttpClient" class="com.http.httpclient.HttpClientFactory">
<constructor-arg>
<bean id="httpConfig" class="com.http.httpclient.HttpClientParamsConfigurationImpl">
<constructor-arg ref="httpProps"/>
</bean>
</constructor-arg>
</bean>
最后一个选项可以使您的代码保持整洁并避免您进行加载等操作。
我的应用程序上下文中有以下 bean:
<bean id="httpClient" factory-method="createHttpClient" class="com.http.httpclient.HttpClientFactory">
<constructor-arg>
<bean id="httpConfig" class="com.http.httpclient.HttpClientParamsConfigurationImpl">
<constructor-arg value="httpclient.properties"/>
</bean>
</constructor-arg>
</bean>
其中 httpclient.properties
是我的属性文件的名称。我在 HttpClientParamsConfigurationImpl
中使用此参数来读取文件(不要介意错误处理太多):
public HttpClientParamsConfigurationImpl(String fileName) {
try(InputStream inputStream = new FileInputStream("resource/src/main/properties/" + fileName)) {
properties.load(inputStream);
} catch (IOException e) {
LOG.error("Could not find properties file");
e.printStackTrace();
}
}
有没有办法在 bean 中传递整个文件位置,这样我就不必在创建 InputStream
时添加路径 resource/src/main/properties
?
我试过 classpath:httpclient.properties
但没用。
你的代码是错误的,文件在类路径中(src/main/resources
被添加到类路径中,其中的文件被复制到类路径的根目录中。在你的例子中,在一个名为 properties
).我建议您使用 Resource
或 Properties
而不是 String
。
public HttpClientParamsConfigurationImpl(Resource res) {
try(InputStream inputStream = res.getInputStream()) {
properties.load(inputStream);
} catch (IOException e) {
LOG.error("Could not find properties file");
e.printStackTrace();
}
}
然后在你的配置中你可以简单地写下:
<bean id="httpClient" factory-method="createHttpClient" class="com.http.httpclient.HttpClientFactory">
<constructor-arg>
<bean id="httpConfig" class="com.http.httpclient.HttpClientParamsConfigurationImpl">
<constructor-arg value="classpath:properties/httpclient.properties"/>
</bean>
</constructor-arg>
</bean>
或者甚至更好,甚至不用加载属性,只需将它们传递给构造函数,让 Spring 为您完成所有硬加载。
public HttpClientParamsConfigurationImpl(final Properties properties) {
this.properties=properties
}
然后使用 util:properties
加载属性并简单地为构造函数引用它。
<util:properties id="httpProps" location="classpath:properties/httpclient.properties" />
<bean id="httpClient" factory-method="createHttpClient" class="com.http.httpclient.HttpClientFactory">
<constructor-arg>
<bean id="httpConfig" class="com.http.httpclient.HttpClientParamsConfigurationImpl">
<constructor-arg ref="httpProps"/>
</bean>
</constructor-arg>
</bean>
最后一个选项可以使您的代码保持整洁并避免您进行加载等操作。