gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:14 24 4
gpt4 key购买 nike

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

配置.jar

  • 开发者
    • inErrorCodes.properties
    • outErrCodes.properties
    • 报告.属性
    • 电子邮件.属性
  • 测试
    • inErrorCodes.properties
    • outErrCodes.properties
    • 报告.属性
    • 电子邮件.属性
  • 产品
    • inErrorCodes.properties
    • outErrCodes.properties
    • 报告.属性
    • 电子邮件.属性

这是 xml 配置的片段:

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

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

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

理想情况下,所有文件中的所有键都是唯一的,但这是一个旧版应用程序。所以我希望完成的是拥有一个数据库表(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/

关于java - 如何通过 Spring 框架从数据库加载应用程序属性 (v4.0.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com