Sunday 16 December 2012

Spring IoC Setter Injection

Posted by Naveen Katiyar On 21:10 1 comment

Spring IoC setter value injection 

We will learn the process of setting property values of a bean using Spring IoC Setter Value Injection. For Spring IoC Setter Value Injection to work the bean class must contain setter of the property present.
In the following example we will consider a Person class which has two properties to construct the name of the person. We will set the property values in Spring IoC Bean Configuration file and the print the name of the person.

Person Bean class (Person.java)


package com.naveen.beans;

public class Person {
    private String firstName = null;
    private String lastName = null;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getName(){
        return firstName + " " + lastName;
    }
}

Person class has two properties “firstName” and “lastName ” and their getter and setters.

Spring IoC Bean Configuration file(spring-servlet.xml)

In configuration file Person class has been configured as a Spring IoC Bean as you seen in the previous tutorial.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="person" class="com.naveen.beans.Person">
        <property name="firstName" value="Carel"/>
        <property name="lastName" value="Staf"/>
    </bean>

</beans>


 
<property/> tag is used to set the value or reference of a property in bean class. This tag has two attributes here. “name” attribute holds the name of the property in bean. In this case there are two properties in Person bean, “firstName” and “lastName”. The “value” attribute holds the value to be set on the property. Spring IoC container will call property setter methods to set the property value.

You can also do the same thing with following bean configuration:

<bean id="person" class="com.naveen.beans.Person">
  <property name="firstName">
      <value>Carel</value>
  </property>
  <property name="lastName">
      <value>Staf</value>
  </property>
</bean>
 

If you want to set null value to a property, then you can use two way to do it :

Both the value will be set as null.

SpringBeanTestRunner class to test the example

Our SpringBeanTestRunner class contains the code to run the example. The runner class first create BeanFactory instance using our Spring IoC Bean Configuration file (spring-servlet.xml) and then get the instance of person bean to call the method “getName’ in it. 


package com.naveen.runner;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.naveen.beans.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        System.out.println("Initialing bean factory");
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-servlet.xml");
        System.out.println("Getting person bean");
        Person person = (Person)beanFactory.getBean("person");
        System.out.println("Person name is " + person.getName());
    }
}

That's it.You can run the previous example after making above changes.



Spring IoC Setter Bean Ref Injection 

In this session we will go through an example that will make you understand Spring IoC Setter Bean Ref Injection. Bean ref injection means to to pass reference of one bean to another. So the example will show the method of setting one bean reference to another bean in Spring IoC Bean Setter Injection.
In the following example, there is a Address bean to store address of a person and we will pass the reference of Address bean to Person bean using Spring IoC Setter Bean Ref Injection.

Address bean (Address.java)

Address bean has two properties and getter/setter of the properties to store city and country of a person.


package com.naveen.beans;

    public class Address {
    private String city = null;
    private String country = null;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "City : " + city + ", Country" + country;
    }
}

Person bean code (Person.java)

Person bean contains three properties “firstName”, “lastname” and “address” and their getter and setter.


package com.naveen.beans;

public class Person {
    private String firstName = null;
    private String lastName = null;
    private Address address = null;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String getName(){
        return firstName + " " + lastName;
    }

    public String getPersonDetail(){
        return "Name :" + getName() + "\n" + address.toString();
    }
}

Spring IoC Bean Configuration file(spring-servlet.xml)

We have configure both the beans Address and Person in our Spring IoC Bean Configuration file.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="address" class="com.naveen.beans.Address">
        <property name="city" value="New York"/>
        <property name="country" value="USA"/>
    </bean>
    <bean id="person" class="com.naveen.beans.Person">
        <property name="firstName" value="Carel"/>
        <property name="lastName" value="Staf"/>
        <property name="address" ref="address"/>
    </bean>
</beans>

You can see only one new thing from previous tutorial.
<property name=”address” ref=”address”/> : Here “ref” attribute says to Spring that Spring has to create the instance of address bean and set to the property of person bean.

SpringBeanTestRunner class to test the example

SpringBeanTestRunner class will get Person instance from Spring IoC Container and will call the method “getPersonDetail” method which will show the Name and Address of the person to test the example.


package com.naveen.runner;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.naveen.beans.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        System.out.println("Initialing bean factory");
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-servlet.xml");
        System.out.println("Getting person bean");
        Person person = (Person)beanFactory.getBean("person");
        System.out.println("Person detail is " + person.getPersonDetail());
    }
}

That's it.You can run the previous example after making above changes. 

1 comments:

Anonymous said...

http://eekshop.com
One can have a many of the new feature on the aspect that is always multi-tasking. With this feature your family can run longer than one tasks at the same a period do nothing more than slightly like a multi function personal laptop repair You can connect easily to explore a number of tablets, TV and laptops about whether or not had to have