如何从多个文件中收集用于单个 bean 的 spring 属性 [英] How to collect spring properties from multiple files for use on a single bean

查看:22
本文介绍了如何从多个文件中收集用于单个 bean 的 spring 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有完全了解 Spring,所以如果这个问题没有意义,请纠正我......

I haven't gotten my head wrapped around Spring yet, so correct me if this question doesn't make sense...

我有一个 PropertyPlaceholderConfigurer

I have a PropertyPlaceholderConfigurer

<bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
    <property name="location" value="classpath:/properties/rdbm.properties" />
</bean>

我猜我注入了一个 bean?

And I have a bean being injected I guess?

<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}" />
    ...

我想要的是使用用户名/密码指向不同属性文件的第二个占位符,以便我可以将属性拆分为两个不同的文件.那么数据库连接信息就可以和db用户名/密码分开了,我可以源控制一个而不是另一个.

What I want is a second placeholder pointing to a different properties file with the username/password so that I can split up the properties into two different files. Then the database connection information can be separate from the db username/password, and I can source control one and not the other.

我基本上尝试使用不同的 id 和文件复制 rdbmPropertiesPlaceholder 并尝试访问属性,但它不起作用.

I've tried basically copying the rdbmPropertiesPlaceholder with a different id and file and trying to access the properties, but it doesn't work.

此代码来自 uPortal 开源门户网站项目.

This code is from the uPortal open source web portal project.

推荐答案

使用此表示法可以指定多个文件:

Using this notation lets you specify multiple files:

 <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>

propertyplaceholderconfigurerer 只是将所有这些合并起来,看起来只有一个,因此您的 bean 定义不知道这些属性来自哪里.

The propertyplaceholderconfigurerer just merges all of these to look like there's only one, so your bean definitions do not know where the properties come from.

这篇关于如何从多个文件中收集用于单个 bean 的 spring 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆