4
votes

I have a scenario where I need to load properties from database or java object into beans.

Consider the example:

<bean id="ConfigAsstDemoBeanParent" class="gps.springconfig.DemoClass" lazy-init="true">
  <property name="demoValueFromBean" value="demoValue"></property>
  <property name="demoValueForKeyFromProperties" value="${DEMO_KEY}"></property>
</bean>

and instead of the ${DEMO_KEY} property placeholder, which loads a value from the property file, I need to load a value from the database, which I retrieve using a stored procedure from a Java class.

Please suggest me a mechanism which I can leverage for the above scenario. Currently I am investigating extending SpringMain and/or PropertyPlaceholderConfigurer class and write my own custom BootStrapper.

Also please suggest hints on writing a BootStrapper for the above mentioned scenario.

1
I use PropertyPlaceholderConfigurer for stuff like this. - Rocky Pulley
@RockyTriton but as I understand PropertyPlaceholderConfigurer only be used to resolve properties from .properties file. I dont really see how it can be used for getting values from DB. Please correct me if I am wrong. - kanap008

1 Answers

7
votes

One of the cases where Java configuration seems to be a great alternative:

@Configuration
public class Config {

    @Resource
    private DataSource dataSource;

    @Bean
    @Lazy
    public DemoClass configAsstDemoBeanParent() {
        DemoClass demo = new DemoClass();
        demo.setDemoValueFromBean("demoValue");
        demo.demoValueForKeyFromProperties( /* query the database here */);
        return demo;
    }

}

Note that you can inject DataSource (or JdbcTemplate) to your @Configuration class providing it was defined elsewhere.