如何通过Spring框架从数据库加载应用程序属性(v4.0.3)

问题描述:

我试图弄清楚如何通过Spring(4.0.3)从数据库表加载我的所有应用程序属性。现在我的应用程序有一组属性文件(大约十几个)。这些属性文件是每个环境的重复(而不是值)。取下面:

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

config.jar


  • dev


    • inErrorCodes.properties

    • outErrCodes.properties

    • report.properties

    • email.properties

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

      • inErrorCodes .properties

      • outErrCodes.properties

      • report.properties

      • email.properties

      • inErrorCodes.properties
      • outErrCodes.properties
      • report.properties
      • email.properties

      • inErrorCodes.properties

      • outErrCodes.properties

      • report.properties

      • email.properties

      • inErrorCodes.properties
      • outErrCodes.properties
      • report.properties
      • email.properties

      这里是xml config的一个片段:

      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"/>
      

      然后在源文件中使用:

      ...
      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 ) {
      

      其他一些警告。 errorCodes文件中的某些属性具有相同的键。例如

      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.
      

      理想情况下,所有密钥在所有文件中都是唯一的,但这是一个遗留应用程序。所以我希望得到的是拥有一个数据库表(来自所有envs的jndi除了本地,它只是一个数据源)。表可能看起来像(表名= APP_PROPERTIES)

      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
      

      其他一些事情。我更喜欢在xml配置上使用注释。我想找到一种方法来使属性可重新加载。如果其中一个值在数据库中更新,那么如果我可以调用Spring函数来重新加载,甚至可能是某些池化机制,那将会很棒。当然,这取代了重新启动应用程序。另外,上面提到的$ {spring.profiles.active}是一个JVM变量(在应用程序服务器控制台中设置),必须在每个环境中设置。任何指针都会非常感激。我在Spring @PropertySource上搜索了一下,但找不到与我正在尝试的内容有关的任何内容。

      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.

      再次感谢,

我希望这可以帮助你,不是4.0.3但是它有效。

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/