0
votes

I have a java.util.Properties object which has few key value pairs in it. I am trying to use this Property object in the spring configuration file i.e. define the key in the spring config and during runtime, it should get the value from the properties object.

For ex:

<bean id="test" class="com.sample.Test">
    <constructor-arg value="${PROPERTY_KEY} />
</bean>

Now during the runtime, the constructor should get the value that is present in the Property object.

Is there a way to get this done ?

Note: I do not want to use config.properties here. Looking to use java.util.Properties

Thanks, Rahul

4
<context:property-placeholder properties-ref="your-properties-bean-id" />. Something like that should do the trick. - M. Deinum
Thanks Denium. I will try this out. But not sure how as I dont have the property bean id in the xml and I cannot create one as well. - RDM
Then where do you have that properties object? And why wouldn't you be able to create it in XML? Can you add more information to your question. - M. Deinum

4 Answers

0
votes
 <context:property-placeholder location="classpath:placeholder.properties"/>

or

<bean id="properties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location" value="classpath:<file-name>.properties" />

</bean>
0
votes

First you have to create bean to access your property file, like below

<bean id="appProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="singleton" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
            <list>
          <value>classpath*:localhost-mysql.properties</value>
          <value>classpath*:mail-server.properties</value>
          </list>
    </property>     
</bean>
<bean id="placeholderConfig"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" ref="appProperties" />
</bean>

Next you can access Key-Value pair from your property files, like below

<bean id="mailServerSettings" class="com.ratormonitor.app.context.EmailSettings">
    <property name="host" value="${mail.server.host}" />
    <property name="port" value="${mail.server.port}" />
    <property name="username" value="${mail.server.username}" />
    <property name="password" value="${mail.server.password}" />
    <property name="requestContextPath" value="${request.context.path}" />
</bean>

Hope this code will solve your problem.

0
votes

You can use Spring Expression Language (SpEL) to get a java object value in spring configuration xml file.

An example :

<property name="misfireInstruction"
                          value="#{T(org.quartz.CronTrigger).MISFIRE_INSTRUCTION_FIRE_ONCE_NOW}"/>
0
votes

So this is how I did:

As I said I had a java.util.Properties object. I then created a CustomProperty class which extended PropertySource>

public class CustomPropertySource extends PropertySource<Map<String, Object>>

Then in my main class I did the following:

AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] {springConfigLocation, false);
context.getEnvironment().getPropertySources.addlast(new CustomPropertySource("custom", propertiesObject));
conext.refresh();

And then in the spring config file, I had to add this:

<context: property-placeholder ignore-unresolvable="true"/>

So in this way, I could fetch values for the keys defined in the spring config file, just like how we get the values from property files.

Thanks, Rahul