Tutorial :How to XML configure Spring bean for constructor injection when bean has varargs constructor



Question:

Is there a way to write a Spring bean in XML so that it uses constructor injection when that constructor has a varargs parameter type? IE, is there a way to specify an array the way you can specify a list?

For instance:

class MyClass {      MyClass(String... args) {          // rest omitted      }  }  


Solution:1

since args is an array of String you can use <list>:

 <bean name="myBean" class="MyClass">      <constructor-arg>          <list>              <value>111</value>              <value>222</value>              <value>333</value>              <value>444</value>          </list>      </constructor-arg>  </bean>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »