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&#8221;
3 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
4 xmlns:aop=”http://www.springframework.org/schema/aop&#8221;
5 xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;
6 <bean id=”person1″ class=”Injection.Person”>
7 <property name=”age” value=”10″ />
8 </bean>
9
10 <bean class=”org.springframework.beans.factory.config.MethodInvokingBean”>
11 <property name=”targetObject”>
12 <ref local=”person1″/>
13 </property>
14 <property name=”targetMethod”>
15 <value>setName</value>
16 </property>
17 <property name=”arguments”>
18 <list>
19 <value>fname</value>
20 <value>lname</value>
21 </list>
22 </property>
23 </bean>
24 </beans>

At line 6 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s