Java 如何从多个文件中收集用于单个 bean 的 spring 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/534199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:55:57  来源:igfitidea点击:

How to collect spring properties from multiple files for use on a single bean

javapropertiesspring

提问by Jonathan Adelson

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

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

I have a PropertyPlaceholderConfigurer

我有一个 PropertyPlaceholderConfigurer

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

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.

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

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

我已经尝试基本上复制具有不同 ID 和文件的 rdbmPropertiesPlaceholder 并尝试访问属性,但它不起作用。

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

此代码来自 uPortal 开源 Web 门户项目。

采纳答案by krosenvold

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>

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.

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

回答by Rolf

The org.springframework.beans.factory.config.PropertyPlaceholderConfigurer can do this (as already answered. What you may want to do is make use of the name spacing so that you can refer to same-named properties from both files without ambiquity. For your example, you can do this:

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>

In your context files, you can now refer to general properties with ${someproperty}, and refer to rdbm properties with $db{someproperty}.

在您的上下文文件中,您现在可以使用${someproperty}引用常规属性,并使用$db{someproperty }引用 rdbm 属性。

This will make your context files much cleaner and clearer to the developer.

这将使您的上下文文件对开发人员更加清晰和清晰。