Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring. How to add same property to multiple beans?

Tags:

java

spring

Consider I have something like this in beans.xml:

<bean id="emails" class="org.some.package.SomeClass">
  <property name="emailList">
  <list>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
  </list>
</property>
</bean>

But I need to add emailList property into multiple beans. How can I do that without writing property to each bean? Can externalize property and inject it into each bean?

I expect something like:

<property name="commonProp">
  <list>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
    <value>[email protected]</value>
  </list>
</property>

<bean id="emailsOne" class="org.some.package.ClassOne">
  <property name="emailList" ref="commonProp" />
</bean>

<bean id="emailsTwo" class="org.some.package.ClassTwo">
  <property name="emailList" ref="commonProp" />
</bean>
like image 202
ipatina Avatar asked Oct 17 '22 14:10

ipatina


1 Answers

You can do it using: util:list

   <util:list id="myList" value-type="java.lang.String"> 
      <value>foo</value> 
      <value>bar</value> 
   </util:list>

Then use this myList reference in other beans.

like image 134
adi Avatar answered Oct 21 '22 04:10

adi