Reputation: 91949
Consider this example
public class EmailSender {
private Properties emailProperties;
public Properties getEmailProperties() {
return emailProperties;
}
public void setEmailProperties(Properties emailProperties) {
this.emailProperties = emailProperties;
}
In applicationContext.xml
I have something like
<bean name="emailSender" class="com.api.email.EmailSender">
<property name="emailProperties" value="classpath*:email.properties"/>
</bean>
When I debug whats get set, I see
How do I load Properties
for emailProperties
?
Upvotes: 0
Views: 80
Reputation: 22553
The way you assign the property file seems incomplete. I normally use the util functions. Add this to your context xml file namespace:
xmlns:util="http://www.springframework.org/schema/util"
Then declare your property file:
<util:properties
id="emailProperties"
location="classpath:/app.properties"/>
And set the bean value:
<bean name="emailSender" class="com.api.email.EmailSender">
<property name="emailProperties" ref="emailProperties"/>
</bean>
Maybe things have been simplified in Spring 4, but that's how you do it in 3 and earlier. it's a teensy bit shorter than using org.springframework.beans.factory.config.PropertiesFactoryBean.
Upvotes: 0
Reputation: 1261
Another solution:
<context:property-placeholder location="classpath*:email.properties" />
<bean class="com.test.EmailSender" >
<property name="prop1" value="${mail.prop1}" />
<property name="prop2" value="${mail.prop2}" />
</bean>
-
public class EmailSender {
private String prop1;
private String prop2;
public String getProp1() {
return prop1;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public String getProp2() {
return prop2;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
}
Upvotes: 0
Reputation: 91949
I had to inject another bean which knows how to resolve Properties. The following worked for me
<bean id="emailProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:email.properties"/>
</bean>
<bean name="emailSender" class="com.api.email.EmailSender">
<property name="emailProperties" ref="emailProperties"/>
</bean>
Upvotes: 1