How to load application properties from a database via Spring framework (v4.0.3)

I am trying to figure out how to load all my application properties from a database table through Spring (4.0.3). right now my application has a set of properties files (about a dozen or so). these property files are duplicated (not 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 use in the 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 of other warnings. some of the properties in errorCodes files have the same key. eg

inErrorCodes.properties
    error.code.1001=bad file name.

outErrCodes.properties
    error.code.1001=bad header info.

, , . , (jndi envs, , ). : ( = 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

. xml config. . , , Spring , , . , , . , ${spring.profiles.active} JVM ( ), ​​ . . Spring @PropertySource, , .

,

+1

Source: https://habr.com/ru/post/1660069/


All Articles