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

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

config.jar

  • 开发
    • 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配置的一个片段:

    

然后在源文件中使用:

 ... 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文件中的某些属性具有相同的键。 例如

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

理想情况下,所有密钥在所有文件中都是唯一的,但这是一个遗留应用程序。 所以我希望得到的是拥有一个数据库表(来自所有envs的jndi除了本地,它只是一个数据源)。 表可能看起来像(表名= 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上搜索了很多,但是找不到任何与我正在尝试的东西有关的东西。

再次感谢,

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

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