简体   繁体   English

如何在带有Spring配置文件的Bean中使用多个通用属性?

[英]How to use multiple common properties in a bean with a Spring config file?

I need to set the properties on a bean with common fields from different "places" so something along the lines of: 我需要在具有来自不同“地方”的公共字段的bean上设置属性,如下所示:

<bean id="parent1" abstract="true">
    <property name="commonField" value="parent1_val"></property>
</bean>

<bean id="parent2" abstract="true">
    <property name="commonField2" value="parent2_val"></property>
</bean>


<bean id="injectDemo1" class="spring.testClasses.InjectDemo1" parent="parent1" parent="parent2>
    <property name="val1" value="val1"/>
</bean>

The above example does not work because only one parent attribute is allowed. 上面的示例不起作用,因为只允许使用一个父属性。 But this is what I need to do; 但这是我需要做的。 is there a mechanism in Spring that will allow me to set properties from multiple sources. Spring中有一种机制可以让我从多个来源设置属性。 I looked at property files to do the same thing but properties files cannot hold Sets, Lists or Maps which I need or can they? 我看着属性文件执行相同的操作,但是属性文件无法容纳我需要的Sets,Lists或Maps,或者它们可以吗?

Something like: 就像是:

<bean id="injectDemo1" class="spring.testClasses.InjectDemo1">
   <property name="val1" value="val1"/>
   <property name="commonField1" value="${prop1}"/>
   <property name="commonField2" value="${prop2}"/>
</bean>

I hope I've explained it okay? 我希望我已经解释了好吗?

我不认为Spring bean支持多重继承,出于同样的原因,Java的制造者决定不支持多重继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM