如何在运行时添加属性并使用它们在我的上下文文件中填充占位符?
首先,一点背景:我有一个加载数据库的批量数据加载器。我要求数据库密码不存储在该程序的磁盘上,因此我允许用户以交互方式输入它。 我正在使用hibernate,我的数据源在上下文文件中配置,实际参数在另一个属性文件中。
我正在寻找的一些属性
db.xml:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
and so on...
</bean>
database.properties:
hibernate.connection.username=Username
### hibernate.connection.password= #don't want the password stored
hibernate.connection.url=<the url>
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
我的主要 context.xml:
<import resource="classpath:db.xml" />
<context:property-placeholder location="classpath:database.properties" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:database.properties" />
</bean>
plus more...
我想要做的是在运行时刷新它之前向上下文添加一个新属性(hibernate.connection.password
),以便在db.xml
中替换相应的值。
我目前的尝试看起来像这样
Properties prop = new Properties();
prop.setProperty("hibernate.connection.password", thePassword);
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setProperties(prop);
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"my-context.xml"}, false); // Don't refresh after loading
ctx.addBeanFactoryPostProcessor(ppc);
ctx.refresh();
但是,我必须做错了,因为我得到一个异常,告诉我我的database.properties
文件中的属性没有被使用。
2016-03-14 14:58:41 WARN ClassPathXmlApplicationContext:546 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [db.xml]: Could not resolve placeholder 'hibernate.connection.driver_class' in string value "${hibernate.connection.driver_class}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'hibernate.connection.driver_class' in string value "${hibernate.connection.driver_class}"
为了确保我对database.properties文件的设置是正确的,如果我删除对ctx.addBeanFactoryPostProcessor(ppc)
的调用,我会收到一个抱怨缺少密码的异常
2016-03-14 16:52:10 WARN ClassPathXmlApplicationContext:546 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [db.xml]: Could not resolve placeholder 'hibernate.connection.password' in string value "${hibernate.connection.password}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'hibernate.connection.password' in string value "${hibernate.connection.password}"
Error: Invalid bean definition with name 'dataSource' defined in class path resource [db.xml]: Could not resolve placeholder 'hibernate.connection.password' in string value "${hibernate.connection.password}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'hibernate.connection.password' in string value "${hibernate.connection.password}"
如果密码出现在属性文件中,一切正常。
那么我的问题是如何使database.properties和运行时定义的属性对我的上下文文件进行后期处理?
修改
我发现的一个解决方案是将属性文件手动加载到PropertyPlaceholderConfigurer中。
ppc.setLocation(new ClassPathResource("database.properties"));
这样可行,但是以编程方式设置的属性将被属性文件中的任何匹配属性覆盖,这使得它感觉像是一种变通方法。另外,我仍然不明白为什么两个PropertyPlaceholderConfigure都没有被使用(在上下文文件中定义的那个和在java中定义的那个)
答案 0 :(得分:1)
为了让Spring从database.properties
读取属性,你必须定义一个属性占位符的bean,如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>database.properties</value>
</property>
</bean>
然后您可以在dataSource
bean中使用它,如下所示:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
and so on...
</bean>
如果要在运行时传递密码,则可以将其作为参数传递,如下所示:
-Dhibernate.connection.password=<password>