Snippets: how to inject a Properties file into a bean using Spring
Here is an example on how inject classes with Spring container.
Let’s suppose you have a bean with a Properties property:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mypackage;
public class MyBeanClass {
Properties myProperties;
//
// other class code
//
//
// don't forget the setter!
//
public void setMyProperties(Properties myProperties) {
this.myProperties = myProperties;
}
}
Here is the two spring beans config for your class and for the Properties bean to be injected.
1
2
3
4
5
6
7
8
9
<bean id="myBeanClassInstance" class="com.mypackage.MyBeanClass" autowire="byName"/>
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/classes/myPropertiesFile.properties</value>
</property>
</bean>
In this example the prop myProperties of the instance myBeanClassInstance reference the properties file /WEB-INF/classes/myPropertiesFile.properties.
See ya!