Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extend a list in spring config

Tags:

I have defined a with some "common" values. How can I extend the common list by additional values to various new beans?

<util:list id="myCommonList" list-class="java.util.LinkedList">  <bean .../>  <bean .../>  <bean .../>  <bean .../> </util:list>   <bean id="extension" parent="myCommonList">   <bean ref="additionalValue"/> </bean> 

Will this overwrite the list or extend it?

like image 736
Bastl Avatar asked Jun 24 '11 07:06

Bastl


People also ask

What is the use of @configuration in spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

How could you externalize constants from a spring configuration file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.


2 Answers

You can do it, but not using <util:list>, which is just a convenience syntax. The container does provide a "collection merging" function, but you have to use the "old" style:

<bean id="parent" class="org.springframework.beans.factory.config.ListFactoryBean">     <property name="sourceList">         <list>             <value>X</value>         </list>     </property> </bean>  <bean id="child" parent="parent" class="org.springframework.beans.factory.config.ListFactoryBean">     <property name="sourceList">         <list merge="true">             <value>Y</value>         </list>     </property> </bean> 
like image 199
skaffman Avatar answered Oct 24 '22 05:10

skaffman


Based on skaffman's answer, you can achive this way:

<util:list id="parent">     <value>X</value> </util:list>  <bean id="child" parent="parent" class="org.springframework.beans.factory.config.ListFactoryBean">     <property name="sourceList">         <list merge="true">             <value>Y</value>         </list>     </property> </bean> 
like image 20
Bird Bird Avatar answered Oct 24 '22 04:10

Bird Bird