如何在springbean中注入完整的propertiesfile
我有一个包含很多值的属性文件,我不想在bean配置文件中单独列出它们。例如:如何在springbean中注入完整的propertiesfile,spring,resources,properties-file,Spring,Resources,Properties File,我有一个包含很多值的属性文件,我不想在bean配置文件中单独列出它们。例如: <property name="foo"> <value>${foo}</value> </property> <property name="bar"> <value>${bar}</value> </property> ${foo} ${bar} 等等 我设想以java.util.Propertie
<property name="foo">
<value>${foo}</value>
</property>
<property name="bar">
<value>${bar}</value>
</property>
${foo}
${bar}
等等
我设想以java.util.Properties
或java.util.Map
的形式完全注入所有内容。
有办法吗?是的,您可以使用
加载属性文件,并将结果java.util.properties
对象声明为bean。然后可以像注入任何其他bean属性一样注入该属性
请参见,以及他们的示例:
<util:properties id="myProps" location="classpath:com/foo/jdbc-production.properties"
可以使用属性OverrideConfiguler
机制:
<context:property-override location="classpath:override.properties"/>
该机制在一节中解释,对于Java配置,请使用PropertiesFactoryBean
:
@Bean
public Properties myProperties() {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/myProperties.properties"));
Properties properties = null;
try {
propertiesFactoryBean.afterPropertiesSet();
properties = propertiesFactoryBean.getObject();
} catch (IOException e) {
log.warn("Cannot load properties file.");
}
return properties;
}
然后,设置properties对象:
@Bean
public AnotherBean myBean() {
AnotherBean myBean = new AnotherBean();
...
myBean.setProperties(myProperties());
...
}
希望这对那些对Java配置方式感兴趣的人有所帮助。对于Java配置,您可以使用以下内容:
@Autowired @Qualifier("myProperties")
private Properties myProps;
@Bean(name="myProperties")
public Properties getMyProperties() throws IOException {
return PropertiesLoaderUtils.loadProperties(
new ClassPathResource("/myProperties.properties"));
}
如果您为每个实例分配一个唯一的bean名称(限定符
),那么您也可以通过这种方式拥有多个属性。这是对这个SO问题的回应的回应。当我将来试图解决这个问题时,我会添加更多的细节来帮助别人和我自己
有三种方法可以插入属性文件
方法1
感谢您的回答-我的错,属性文件将由“非开发人员”编辑,并且对于没有特殊背景的编辑人员来说应该“易于阅读”。因此,bean的名称不是用作键的正确方式。哇,这是很多代码。难道没有一种更简单的方法像1行XML配置
?@rustyx噢!当时我没有找到任何其他的方法,但是非常好!:)款待就像其他的豆子一样。在xml配置中使用它,使用ref=myProps
或Autowire a属性让Spring为您处理它。
@Autowired @Qualifier("myProperties")
private Properties myProps;
@Bean(name="myProperties")
public Properties getMyProperties() throws IOException {
return PropertiesLoaderUtils.loadProperties(
new ClassPathResource("/myProperties.properties"));
}
<bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:com/foo/jdbc-production.properties</value>
</list>
</property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans
...
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="...
...
http://www.springframework.org/schema/util/spring-util.xsd"/>
<util:properties id="myProps" location="classpath:com/foo/jdbc-production.properties"/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:com/foo/jdbc-production.properties" />
</bean>
@Value("#{myProps[myPropName]}")
private String myField;