Example:

  1. Create the Employee class.
  2. package com.java2learn.core;
    
    public class Employee {
    	
    	private String name;
    	private int age;
    	private double salary;
    	
    	//injecting name,age and salary in the constructor
    	public Employee(String name, int age, double salary) {
    		this.name = name;
    		this.age = age;
    		this.salary = salary;
    	}
    
    	public String getName() {
    		return name;
    	}
    	
    	public int getAge() {
    		return age;
    	}
    	
    	public double getSalary() {
    		return salary;
    	}
    
    	@Override
    	public String toString() {
    		return "Employee [name=" + name + ", age=" + age + ", salary=" + salary
    				+ "]";
    	}
    }
    
    
  3. Create the Company class(POJO).
  4. package com.java2learn.core;
    
    public class Company {
    
    	private String name;
    	private String compnayType;
    	private Employee employee;
    
    	//injecting name,compnayType and employee in the constructor
    	public Company(String name, String compnayType, Employee employee) {
    		
    		this.name = name;
    		this.compnayType = compnayType;
    		this.employee = employee;
    	}
    
    	public Employee getEmployee() {
    		return employee;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getCompnayType() {
    		return compnayType;
    	}
    
    	@Override
    	public String toString() {
    		return "Company [name=" + name + ", compnayType=" + compnayType
    				+ ", employee=" + employee + "]";
    	}
    }
    
  5. Create the Configuration file and add Company and Employee POJO’s to the 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">
    		<constructor-arg name="name" value="Rock"></constructor-arg>
    		<constructor-arg name="age">
    			<value>52</value>
    		</constructor-arg>
    		<constructor-arg name="salary" value="521352.4"></constructor-arg>
    
    
    	</bean>
    
    	<bean id="company" class="com.java2learn.core.Company">
    		<constructor-arg name="name" value="GOOGLE"></constructor-arg>
    		<constructor-arg name="compnayType">
    			<value>PRODUCT</value>
    		</constructor-arg>
    		<constructor-arg ref="employee"></constructor-arg>
    	</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 ::"+company);
    		System.out.println("Employee::"+company.getEmployee());
    		System.out.println("Employee Salary::"+company.getEmployee().getSalary());
    	}
    }
    

Output:

Company ::Company [name=GOOGLE, compnayType=PRODUCT, employee=Employee [name=Rock, age=52, salary=521352.4]]
Employee::Employee [name=Rock, age=52, salary=521352.4]
Employee Salary::521352.4