Setter injection example with more than one argument

This post explains how to instruct spring to call a setter method (which take more than one argument) while creating a bean.

In most of the cases the setter methods of a class takes only one argument but if a case arises where a setter method takes more than one argument and the class
creation has to be configured in spring xml, the below approach will help developers.

The Person class contains setter method named “setName” and it takes two arguments fname and lname. Refer to line 15.

Person


1 package Injection;
2
3 import java.io.Serializable;
4
5 public class Person implements Serializable {
6 private String fname;
7 private String lname;
8 private String name;
9 private int age;
10
11 public String getName() {
12 return name;
13 }
14
15 public void setName(String fname, String lname) {
16 this.name = fname + lname;
17 }
18
19 public int getAge() {
20 return age;
21 }
22
23 public void setAge(int age) {
24 this.age = age;
25 }
26
27 @Override
28 public String toString() {
29 String data = this.name + ":" + this.age;
30 return data;
31 }
32 }

In the spring configuration file, we create a bean declaration for the Person class as shown below

injection.xml


1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
5   <bean id="person1" class="Injection.Person">
6       <property name="age" value="10"/>
7   </bean>
8   
9   <bean id="methodInvokingBean" class="org.springframework.beans.factory.config.MethodInvokingBean">
10      <property name="targetObject" ref="person1"/>
11          <property name="targetMethod" value="setName"/>
12          <property name="arguments">
13          <list>
14              <value>fname</value>
15              <value>lname</value>
16          </list>
17      </property>
18  </bean>
19</beans>

At line 5 we declare a bean, set the age property, but don’t set the name property. We set the name property by the help of spring class “MethodInvokingBean”.

The class MethodInvokingBean takes three argument
1) the instance on which target method should be called
2) the target method name to be called
3) the arguments that should be passed to target method when being called

Main Code


1 package Injection;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 public class SpringInjectionDemo1 {
7 public static void main(String[] args) throws Exception {
8 ApplicationContext context = new ClassPathXmlApplicationContext("injection.xml");
9
10 Person person1 = (Person)context.getBean("person1");
11
12 System.out.println(person1);
13 }
14 }

When the application context is created, MethodInvokingBean is called during the context creation and target method is called on target class with specified
arguments.

Output

fnamelname:10

Leave a Reply