java Spring 从属性 bean 引用一个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14260328/
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-10-31 15:39:11  来源:igfitidea点击:

Spring reference a property from a property bean

javaspringdependency-properties

提问by Michael

I have created a spring configuration file that works well.

我创建了一个运行良好的 spring 配置文件。

My next step was to separate user configuration properties from system properties.

我的下一步是将用户配置属性与系统属性分开。

I have decided to create additional xml file with beans that will be configured by the user.

我决定使用将由用户配置的 bean 创建额外的 xml 文件。

I had problem to create few such logical beans encapsulating properties that will be used by real class beans:

我在创建几个这样的逻辑 bean 时遇到了问题,这些逻辑 bean 封装了真正的类 bean 将使用的属性:

I have found over the net an option to reference proprieties in such way:

我在网上找到了一个以这种方式引用属性的选项:

UserConf.xml

用户配置文件

<bean id="numberGuess" class="x...">
    <property name="randomNumber" value="5"/>

    <!-- other properties -->
</bean>

SystemConf.xml

系统配置文件

<import resource="UserConf.xml" />

<bean id="shapeGuess" class="y...">
    <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>

    <!-- other properties -->
</bean>

But my problem is that i need x...class to be something logical that shouldn't be initialized at all, and i don't want it to disclose any info of the class hierarchy of the system since it should be only in use configuration xml file.

但我的问题是我需要x...类是完全不应该被初始化的逻辑,我不希望它透露系统的类层次结构的任何信息,因为它应该只在使用配置 xml 文件中。

Solution1is to create a Java object representing this proprieties:

解决方案 1是创建一个代表此属性的 Java 对象:

public class MyProps(...)

and add a bean parent in the spring system configuration:

并在 spring 系统配置中添加一个 bean parent:

<bean id="MyProps" class="path to MyProps"/>

in the user side change the previous bean to be:

在用户端将之前的 bean 更改为:

<bean id="numberGuess" parent="MyProps">
    <property name="randomNumber" value="5"/>

    <!-- other properties -->
</bean>

Solution2is to use flat configuration file just like Database.props, and load it using factory.

解决方案2是像Database.props一样使用flat配置文件,使用factory加载。

Solution3is to use Spring Property Placeholder configuration to load properties from XML properties file (e.g. example), but here i simply don't know how to get a more complex nested structure of properties (properties need to be separated by different logical names, e.g. minNumber will be defined both under xAlgo and y algo).

解决方案3是使用Spring Property Placeholder配置从XML属性文件中加载属性(例如example),但是这里我根本不知道如何获得更复杂的属性嵌套结构(属性需要用不同的逻辑名称分隔,例如minNumber 将在 xAlgo 和 y 算法下定义)。

I don't like to create new Java class only to deal with this problem or to move my user configuration to a flat props file (i need the xml structure), is their any other solution??

我不喜欢创建新的 Java 类只是为了处理这个问题或将我的用户配置移动到一个平面 props 文件(我需要 xml 结构),他们还有其他解决方案吗?

采纳答案by Michael

I will answer my own question, since it looks as the best solution for me (and much more simplistic than was suggested)

我会回答我自己的问题,因为它对我来说是最好的解决方案(而且比建议的要简单得多)

I will use PropertiesFactoryBean to do the work for me:

我将使用 PropertiesFactoryBean 为我完成这项工作:

e.g.

例如

UserConf.xml

用户配置文件

<bean id="numberGuess" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="properties">
    <props>
        <prop key="randomNumber">3</prop>

    <!-- other properties -->
</bean>

SystemConf.xml

系统配置文件

<import resource="UserConf.xml" />

<bean id="shapeGuess" class="y...">
    <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>

    <!-- other properties -->
</bean>

回答by Adam Gent

First if you don't know about the property place holderyou should take a look at that. Also @Value("${some.property:defaultvalue}")is something you should look at.

首先,如果您不了解属性占位符,您应该看看它。也是@Value("${some.property:defaultvalue}")你应该看的东西。

Second the word configurationis ambiguous in Spring. Spring uses this word but they mean developer configuration not user configuration. Despite what people say or think Spring is not a configuration engine.

其次,Spring 中的配置这个词是不明确的。Spring 使用这个词,但它们的意思是开发人员配置而不是用户配置。不管人们怎么说或认为 Spring 不是配置引擎。

I'm not sure what your trying to do but you should be aware that your configuration will not be adjusted at runtime which is frequently needed for something like "user" configuration. So most people write their own configuration layer.

我不确定你想做什么,但你应该知道你的配置不会在运行时调整,这是“用户”配置之类的经常需要的。所以大部分人自己写配置层。

Another thing you should take a look at is not using the XML configuration and instead use Java Configurationwhich will give you way more flexibility.

您应该考虑的另一件事是不使用 XML 配置,而是使用Java 配置,这将为您提供更大的灵活性。