1

I am trying to figure out how to load all my application properties from a database table via Spring (4.0.3). right now my application has a set of property files (approx a dozen or so). these property files are duplicated (not the values) for each environment. take below:

config.jar

  • dev
    • inErrorCodes.properties
    • outErrCodes.properties
    • report.properties
    • email.properties
  • test
    • inErrorCodes.properties
    • outErrCodes.properties
    • report.properties
    • email.properties
  • prod
    • inErrorCodes.properties
    • outErrCodes.properties
    • report.properties
    • email.properties

and here is a snippet from xml config:

<util:properties id="inboundErrorCodes" location="classpath:config/${spring.profiles.active}/inErrCodes.properties"/> <util:properties id="outboundErrorCodes" location="classpath:config/${spring.profiles.active}/outErrCodes.properties"/> <util:properties id="reportProperties" location="classpath:config/${spring.profiles.active}/report.properties"/> <util:properties id="emailProperties" location="classpath:config/${spring.profiles.active}/email.properties"/> 

and then the usage in a source file:

... import javax.inject.Inject; import javax.inject.Named; @Named("testService") public class TestServiceImpl implements TestService { private Properties inboundErrorCodes = null; private Properties outboundErrorCodes = null; private Properties reportProperties = null; private Properties emailProperties = null; @Inject public TestServiceImpl(@Named("inboundErrorCodes") final Properties inboundErrorCodes, @Named("outboundErrorCodes") final Properties outboundErrorCodes, @Named("reportProperties") final Properties reportProperties, @Named("emailProperties") final Properties emailProperties ) { 

a couple other caveats. some of the properties in the errorCodes files have the same key. for example

inErrorCodes.properties error.code.1001=bad file name. outErrCodes.properties error.code.1001=bad header info. 

ideally, all keys would be unique across all files, but this is a legacy app. so what I was hoping to accomplish was to have a database table (jndi from all envs except local, where it would just be a datasource). the table might look like (table name=APP_PROPERTIES)

id key value category == =============== ============= ============ 1 error.code.1001 bad file name. inErrorCodes 2 error.code.1001 bad header info. outErrorCodes 3 default.subject Successful order email 4 sales.title NE Sales Region report 

A couple other things. I would prefer to use annotations over xml config. And I would like to find a way to make the properties reloadable. if one of the values was updated in the database, it would be great if I could call a Spring function to reload, or maybe even some pooling mechanism. this is, of course, in lieu of restarting the application. also, the ${spring.profiles.active} noted above is a JVM variable (set in application server console) that must be set in every environment. any pointers would be really appreciated. I searched quite a bit on the Spring @PropertySource, but couldn't find anything really related to what I was trying.

thanks again,

1 Answer 1

0

I hope this could help you, it not for 4.0.3 but it works.

http://pure-essence.net/2011/02/10/spring-loading-properties-from-database-with-a-twist/

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for pointing me to this resource. it definitely helps me get a better understanding of how to both wire up property files and properties from a database table. I was hoping to do most of the spring configuration via annotations. any ideas related to the PropertySource Spring annotation? thank you again for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.