【问题标题】:ApplicationContext.xml does not update env variables at targetApplicationContext.xml 不更新目标的环境变量
【发布时间】:2019-07-01 05:33:24
【问题描述】:

这是我的 ApplicationContext.xml 代码

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
                <property name="driverClassName" value="${database.driverClassName}" />
                <property name="url" value="${database.url}" />
                <property name="username" value="${database.username}" />
                <property name="password" value="${database.password}" />
                <property name="testOnBorrow" value="true" />
                <property name="testOnReturn" value="true" />
                <property name="testWhileIdle" value="true" />
                <property name="timeBetweenEvictionRunsMillis" value="1800000" />
                <property name="numTestsPerEvictionRun" value="3" />
                <property name="minEvictableIdleTimeMillis" value="1800000" />
            </bean>

我的属性在一个名为 database.properties

的文件中定义

我需要在 父 pom.xml 中进行哪些更改才能在 目标运行时

转换 env 变量

能否请您帮忙或给我一个适当的建议或链接,让我在运行时获得数据库属性的名称。 例如 database.driverName 应更新为 jdbcDriver

【问题讨论】:

    标签: spring hibernate datasource applicationcontext


    【解决方案1】:
    @Component
    public class PropertyReloader {
    
        @Autowired
        private StandardEnvironment env;
    
        @Scheduled(fixedRate=5000)
        public void reloadProperties() throws IOException {
            MutablePropertySources ps = env.getPropertySources();
            Properties pr = new Properties();
            InputStream inputStream = getClass().getResourceAsStream("/ApplicationContext.xml");
            pr.load(inputStream);
            inputStream.close();
            pr.replace("class path resource [ApplicationContext.xml]", new PropertiesPropertySource("class path resource [ApplicationContext.xml]", pr));
        }
    }
    

    这将每五秒更新一次。

    【讨论】:

    • 我的父 pom.xml 有 env 标签和在其中定义的路径,但在目标上没有调用实际名称。
    【解决方案2】:

    因为我的项目必须有各自的.properties 文件,而我的父 pom.xml 只使用 .properties 文件。它无法获取其他 .properties 文件。我的变量在运行时没有得到更新的原因。我维护了一个common.properties 文件并在pom.xml 中添加了路径

    现在我得到了想要的结果

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2020-06-06
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多