1. The setter-based injection uses the setter property in beans for injecting values.
  2. Primitive values and reference values can be injected through the setter property.

Example:

  1. Create the Company POJO.
  2. package com.java2learn.core;
    
    public class Company {
    	
    	private String name;
    	private String compnayType;
    	private Employee employee;
    	
    	public Employee getEmployee() {
    		return employee;
    	}
    	//setting employee as a setter method
    	public void setEmployee(Employee employee) {
    		this.employee = employee;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getCompnayType() {
    		return compnayType;
    	}
    	public void setCompnayType(String compnayType) {
    		this.compnayType = compnayType;
    	}
    }
    
    
  3. Create the Employee POJO.
  4. package com.java2learn.core;
    
    public class Employee {
    	
    	private String name;
    	private int age;
    	private double salary;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public double getSalary() {
    		return salary;
    	}
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    
    }
    
  5. Add Company and Employee POJO’s in the Spring configuration file.
  6. <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<bean id="employee" class="com.java2learn.core.Employee">
    		<property name="name" value="Moon"></property>
    		<property name="age">
    			<value>10</value>
    		</property>
    		<property name="salary" value="350000"></property>
    	</bean>
    
    	<bean id="company" class="com.java2learn.core.Company">
    		<property name="name" value="Oracle"></property>
    		<property name="compnayType" value="serviceBased"></property>
    		<property name="employee" ref="employee"></property>
    	</bean>
    
    </beans>
    
  7. Create the client and run it.
  8. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    import com.java2learn.core.Company;
    
    
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    	
    		//ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-Core.xml");
    		//(or)
    		ApplicationContext applicationContext=new FileSystemXmlApplicationContext("classpath:Spring-Core.xml");
    		Company company=(Company)applicationContext.getBean("company");
    		System.out.println("Company name::"+company.getName());
    		System.out.println("Employee name::"+company.getEmployee().getName());
    		System.out.println("Employee Salary::"+company.getEmployee().getSalary());
    	}
    }
    
    

Output:

Company name::Oracle
Employee name::Moon
Employee Salary::350000.0