开发者

How to load data from property file into bean property values?

开发者 https://www.devze.com 2023-04-04 19:18 出处:网络
I am following the following article. http://www.mkyong.com/spring/spring-quartz-scheduler-example/ Everything works fine.

I am following the following article.

http://www.mkyong.com/spring/spring-quartz-scheduler-example/

Everything works fine.

<bean id="simpleTrigger"

    class="org.springframework.scheduling.quartz.SimpleTriggerBean">

    <property name="jobDetail" ref="runMeJob" />
    <property name="repeatInterval" value="5000" />
    <property name="startDelay" value开发者_如何学编程="1000" />

</bean>

I created a property file app.properties which has

 repeatInterval = 5000
 startDelay = 1000

I want to load these data into bean properties. Right now I have to hard code the values into the xml file.

I want to be able to load the data from property file into the bean properties. Is it possible?

EDIT:

I have

<property name="repeatInterval" value="5000" />

What I am looking for is a way to make it

<property name="repeatInterval" value= "get 5000 from property file" />


To find a file myPropertyFileName.properties that is on your classpath and load it into your spring config, create the following bean:

<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="classpath:myPropertyFileName.properties"/>
  <property name="placeholderPrefix" value="${props:"/>
</bean>

Then use a property name defined like

repeatInterval=5000 

like this:

<property name="repeatInterval" value="${props:repeatInterval}"/>


Use Spring propertyPlaceholderConfigurer to achieve this. Follow this guide.


I have run into something similar in the past. I needed to load a bunch of beans using Spring but I wanted them to be user editable bean files. So I didn't want to include them in the jar packaging. What I did was create my user-defined bean files outside the jar but in a know relative location. My packaged bean definition file referenced the beans defined in the user-defined bean file and when I loaded the application context I provided both files (user-defined & packaged).

Bit unorthodox but it worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号