×
☰ See All Chapters

Setter Injection in Spring

The Setter method injection approach of dependency injection is the way of injecting the dependencies of an object by using the setter method. Writing setter method is mandatory. The setter method injection is more convenient to inject more number of dependencies since number of arguments makes constructor awkward. The <property> sub element of <bean> is used for setter injection. (spring configuration file). When setter injection is used presence of default constructor is mandatory a javabean class. The <property> element is used to describe one-setter method which should have one and only one parameter for particular property. All child tags of <property> elements are same as the child tags of <constructor-arg> element. And usage of child tags of <property> elements are same as that of child tags of <constructor-arg> element.

Setter Injection Example

Let's see the simple example to inject primitive and object-based values. To inject primitive values we should use value attribute and to inject object values we should use ref attribute of <property>. While injecting object values ref attribute should be assigned with id value of other beans.

Project Directory Structure

setter-injection-0
 

EmplyeeAdress.java

package com.java4coding;

 

public class EmplyeeAdress {

        public String city;

 

        public String getCity() {

                return city;

        }

 

        public void setCity(String city) {

                this.city = city;

        }

       

}

EmployeeBean.java

package com.java4coding;

 

public class EmployeeBean {

        public String name;

        public EmplyeeAdress add;

       

        public String getName() {

                return name;

        }

        public void setName(String name) {

                this.name = name;

        }

        public EmplyeeAdress getAdd() {

                return add;

        }

        public void setAdd(EmplyeeAdress add) {

                this.add = add;

        }

}

 

 

EmployeeTest.java

package com.java4coding;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class EmployeeTest {

        public static void main(String[] args) {

                BeanFactory factory = new ClassPathXmlApplicationContext("springConfig.xml");

                EmployeeBean employeeBean = (EmployeeBean) factory.getBean("studentbean");

                System.out.println(employeeBean.getAdd().getCity());

                System.out.println(employeeBean.getName());

        }

}

springConfig.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <property name="city" value="Bangalore"></property>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean">

                <property name="add" ref="adrress"></property>

                <property name="name" value="Manu Manjunatha"></property>

        </bean>

 

</beans>

 

Output

Bangalore

Manu Manjunatha


All Chapters
Author