Java 如何从多个文件中收集spring属性以在单个bean上使用
我还没有完全了解春天,所以如果这个问题没有意义,请纠正我 我有一个PropertyPlaceHolderConfigurationJava 如何从多个文件中收集spring属性以在单个bean上使用,java,properties,spring,Java,Properties,Spring,我还没有完全了解春天,所以如果这个问题没有意义,请纠正我 我有一个PropertyPlaceHolderConfiguration <bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false"> <property name="location" value="clas
<bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
<property name="location" value="classpath:/properties/rdbm.properties" />
</bean>
我想我注射了一颗豆子
<bean id="PortalDb" 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}" />
...
...
我想要的是第二个占位符,它用用户名/密码指向不同的属性文件,这样我就可以将属性拆分为两个不同的文件。然后数据库连接信息可以与数据库用户名/密码分开,我可以对其中一个进行源代码控制,而不是另一个
我尝试过用不同的id和文件复制rdbmPropertiesPlaceholder并尝试访问这些属性,但没有成功
此代码来自uPortal开源门户网站项目。使用此符号可以指定多个文件:
<bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
<property name="locations">
<list>
<value>classpath:/properties/rdbm.properties</value>
<value>classpath:/properties/passwords.properties</value>
</list>
</property>
</bean>
类路径:/properties/rdbm.properties
类路径:/properties/passwords.properties
PropertyPlaceholderConfigurer只是合并了所有这些,看起来只有一个,因此您的bean定义不知道属性来自何处。org.springframework.beans.factory.config.PropertyPlaceholderConfigurer可以做到这一点(如前所述。您可能需要使用名称间距,以便可以从两个文件中引用相同的命名属性,而不会产生歧义。对于您的示例,您可以这样做:
<bean id="generalPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/properties/general.properties"/>
</bean>
<bean id="db.PropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/properties/rdbm.properties" />
<property name="placeholderPrefix" value="$db{" />
<property name="placeholderSuffix" value="}" />
</bean>
在上下文文件中,现在可以使用${someproperty}引用常规属性,并使用$db{someproperty}引用rdbm属性
这将使您的上下文文件对开发人员更加清晰。该语法似乎还可以,但它似乎没有加载我的第二个文件。不太确定发生了什么…好的,所以我想我已经解决了。在两个地方做了相同的事情,但它似乎只对我没有看到的一个有影响。谢谢,我认为这是f确定了!如果我想获得
.properties
文件的内容,即@Value(${content.of.properties.file}”)
,也可以看到这一点吗?