Spring comma-separated list of bean links

In Spring, is it possible to list a bean list separated by commas in another bean, ideally without any custom property editors. This list comes from a placeholder that I do not control.

For example (does not work):

<bean id="bean1" class="java.lang.Integer /> <bean id="bean2" class="java.lang.Integer /> <bean class="customclass"> <constructor-arg><ref bean="bean1,bean2" /></constructor-arg> </bean> 

Which would be identical:

 <bean id="bean1" class="java.lang.Integer /> <bean id="bean2" class="java.lang.Integer /> <bean class="customclass"> <constructor-arg> <list> <ref bean="bean1"/> <ref bean="bean2"/> </list> </constructor-arg> </bean> 
+4
source share
2 answers

It is possible to use this approach:

Consider a properties file with the following elements:

test.properties :

 beanlist1=#{{@bean1,@bean2}} 

Now you can do it:

 <context:property-placeholder location="test.properties"/> <bean id="bean1" class="java.lang.Integer /> <bean id="bean2" class="java.lang.Integer /> <bean class="customclass"> <constructor-arg value="${beanlist}></constructor-arg> </bean> 

which is close enough to what you want (a slightly different view is #{{@bean1,@bean2}} instead of bean1,bean2 ).

Another way is the following:

 <bean class="customclass"> <constructor-arg value="#{{@bean1,@bean2}}"></constructor-arg> </bean> 

Both work using the Spring -EL expression to represent the list.

+5
source

Not directly, but it would be enough to write a factory bean to do it for you. He will need to take a comma separated string as a property, split it and return a ManagedList containing a RuntimeBeanReference for each name. Then it could be entered into other beans in the usual way, and the links will be resolved as you need.

0
source

Source: https://habr.com/ru/post/1433065/


All Articles