0

I asked a similar question, but based on the responses, I did a bad job describing what I am after. I have a spring 4 webapp that loads properties from a properties file. We consume those properties both via the "${proper.name"} expressions in spring, as well as by injecting a properties object into some of our classes.

We want to move most of the properties to a database table and make them reloadable. However, a few need to stay in local properties, potentially overriding the database setting. These should also be loaded dynamically after the app is running.

I know that once a particular bean is injected, it won't get reloaded, that doesn't concern me, it's up to that module to handle that. But I am having trouble getting the behavior I want. In particular, I have implemented an AbstractConfiguration from apache commons configuration to get the dual source and overriding I am after. But while it works for injecting the properties object, expressions loaded with "${prop.name}" don't work at all.

How can I get them to work? Did I override the wrong thing? Is it just some config detail?

<bean id="sysProperties" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="databaseConfigurator" />
    <property name="targetMethod" value="getProperties"/>
</bean> 

<bean id="databaseConfigurator" class="my.util.config.MyDatabaseConfigurator">
    <property name="datasource" ref="dataSource" />
    <property name="propertyFile" value="/WEB-INF/my.properties" />
    <property name="applicationName" value="ThisApp" />
</bean>

<bean id="dbConfigFactory" class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
    <constructor-arg ref="databaseConfigurator" />
</bean> 
Entropy
  • 1,219
  • 6
  • 21
  • 45
  • Possible duplicate of [Store properties in database, but override locally](http://stackoverflow.com/questions/35850768/store-properties-in-database-but-override-locally) – JEY Mar 08 '16 at 15:51
  • I don't think it is because I asked that question and I think I did a bad job asking it. The responder to that question got stuck on the jndi issue because I focused on it too much in the question. The result is that I got no useful answers. So I rephrased. @JEY – Entropy Mar 10 '16 at 17:31

1 Answers1

0

I haven't tested this, but I think it might work.

<bean id="sysProperties" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="databaseConfigurator" />
    <property name="targetMethod" value="getProperties"/>
</bean> 

<bean id="databaseConfigurator" class="my.util.config.MyDatabaseConfigurator">
    <property name="datasource" ref="dataSource" />
    <property name="propertyFile" value="/WEB-INF/my.properties" />
    <property name="applicationName" value="ThisApp" />
</bean>



<bean name="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" ref="CommonsConfigurationFactoryBean"/>
</bean>

<bean name="CommonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
       <constructor-arg ref="databaseConfigurator"/>
</bean>
MarkOfHall
  • 3,334
  • 1
  • 26
  • 30
  • org.springmodules.commons http://mvnrepository.com/artifact/org.springmodules/spring-modules-jakarta-commons – MarkOfHall Mar 08 '16 at 16:07
  • I hadn't tried that springmodules subpackage, but the results appear about the same (the ${} notation properties don't work including the jndi name on the datasource). Is this basically a derivative of apache commons configuration? MyDatabaseConfigurator inherits from org.apache.commons.configuration.AbstractConfiguration. Perhaps there is a springmodules variant I need to inherit from? – Entropy Mar 10 '16 at 18:46
  • You need to add more detail to the question. Where are you using ${} notation? You also need to provide more detail about your spring config. Are there multiple Spring Contexts involved? Is the bean using ${} in the same context as the PropertyPlaceholderConfigurer? – MarkOfHall Mar 10 '16 at 19:22
  • We use ${} whenever we want a property injected. Such as the jndi datasource name or the cron expression we use to prime our quartz tasks. Works fine if I use a plain property file, doesn't work at all when I go to this database drive property file. Only one spring context, so yes to the last question. I can see the DB config loading the properties, and they are available in the 'sysProperties' as a whole when I inject that, but the quartz tasks don't start, and the jndi name is never recognized. The jndi name makes sense since its on the datasource fed into this bean. – Entropy Mar 14 '16 at 13:50
  • ...but even when I hardcode the jndi name in the spring config, the quartz tasks won't load...as if they received a null or blank cron expression. – Entropy Mar 14 '16 at 13:51