java - 在 Spring 中读取双重/多重属性

标签 java spring spring-mvc properties spring-boot

我正在尝试从数据库读取应用程序特定的属性,并且我正在尝试将数据库凭据保留在属性文件中。

So I need to load both properties (File and DB), while the application is loading.

我的配置如下。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="WEB-INF/config/db.properties"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="properties">
        <bean class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
            <constructor-arg>
                <bean class="org.apache.commons.configuration.DatabaseConfiguration">
                    <constructor-arg type="javax.sql.DataSource" ref="postgresqlDataSource"/>
                    <constructor-arg value="TBL_APP_SETTINGS"/>
                    <constructor-arg value="PROP_KEY"/>
                    <constructor-arg value="PROP_VALUE"/>
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>
<bean id="postgresqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="url" value="${db.url}"/>
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="username" value="${db.user}"/>
    <property name="password" value="${db.pass}"/>
    <property name="initialSize" value="1"/>
    <property name="maxActive" value="5"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="timeBetweenEvictionRunsMillis" value="5000"/>
    <property name="removeAbandonedTimeout" value="60"/>
    <property name="minEvictableIdleTimeMillis" value="30000"/>
    <property name="minIdle" value="3"/>
</bean>

${db.url} 抛出错误

请帮忙怎么做?提前致谢。

最佳答案

最简单的方法是将指南中的一些配置属性外部化 spring.io site 。您可以将属性作为 java 系统属性或命令行参数传递

第二种方法是编写自己的 PropertyPlaceholderConfigurer ,它将从 application.yml 和数据库读取属性

关于java - 在 Spring 中读取双重/多重属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422884/

相关文章:

java - 为什么在将 Mono<Void> 映射到 Mono<String> 时收到空字符串?

java - spring认证管理器不绑定(bind)@Service注解的用户服务

Java hibernate 过滤器不工作

java - 正则表达式根据确切的字符拆分,除非在它之前用反斜杠转义

java - 我怎么知道正则表达式是否有多个可能的匹配项?

sql - 如何在 node.js 中使用 Spring MVC

java - 如何以及何时使用 Mockito Annotations 和 jUnit 进行清理

java - 如何隐式选择viewResolver

java - Spring BeanPostProcessor中的postProcessBeforeInitialization是什么意思?

java - 无法转换为 org.springframework.security.core.userdetails.User