Reference Type Beans In Spring Example

Step 1 : create new Maven Project in Eclipse

Step 2 : In pom.xml file update below dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sv</groupId>
  <artifactId>spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring</name>
  <url>http://maven.apache.org</url>

   <properties>
        <springframework.version>4.3.6.RELEASE</springframework.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Step 3 : write Laptop class as :

package com.sv.spring.refbeans;

public class Laptop {

	private String laptopId;
	private String laptopModel;
	private String laptopPrice;
	public String getLaptopId() {
		return laptopId;
	}
	public void setLaptopId(String laptopId) {
		this.laptopId = laptopId;
	}
	public String getLaptopModel() {
		return laptopModel;
	}
	public void setLaptopModel(String laptopModel) {
		this.laptopModel = laptopModel;
	}
	public String getLaptopPrice() {
		return laptopPrice;
	}
	public void setLaptopPrice(String laptopPrice) {
		this.laptopPrice = laptopPrice;
	}
	public Laptop(String laptopId, String laptopModel, String laptopPrice) {
		super();
		this.laptopId = laptopId;
		this.laptopModel = laptopModel;
		this.laptopPrice = laptopPrice;
	}
	public Laptop() {
		super();
	}
	@Override
	public String toString() {
		return "Laptop [laptopId=" + laptopId + ", laptopModel=" + laptopModel + ", laptopPrice=" + laptopPrice + "]";
	}
	
	
	
}

Step 4 : write Employee class as below :

package com.sv.spring.refbeans;

public class Employee {

	private String empId;
	private String empName;
	private Laptop laptop;
	public String getEmpId() {
		return empId;
	}
	public void setEmpId(String empId) {
		this.empId = empId;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public Laptop getLaptop() {
		return laptop;
	}
	public void setLaptop(Laptop laptop) {
		this.laptop = laptop;
	}
	public Employee(String empId, String empName, Laptop laptop) {
		super();
		this.empId = empId;
		this.empName = empName;
		this.laptop = laptop;
	}
	public Employee() {
		super();
	}
	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName + ", laptop=" + laptop + "]";
	}
	
	
	
	
}

Step 5 : write springconfig.xml file as below :

<?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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


	<bean id="lenovo" class="com.sv.spring.refbeans.Laptop"
		p:laptopId="122" p:laptopModel="z580" p:laptopPrice="100009" />

	<bean id="emp1" class="com.sv.spring.refbeans.Employee"
		p:empId="ID-101" p:empName="Swapnil">
		<property name="laptop">
			<ref bean="lenovo" />
		</property>
	</bean>

</beans>

In above spring configuration file we created two beans “lenovo” and “emp1” we initialized properties of lenovo bean by using p-schema and for emp1 bean we defined lenovo as reference bean inside <ref> tag as above.

Step 6 : Write test class and get bean :

 package com.sv.spring.refbeans;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"com/sv/spring/refbeans/springconfig.xml");

		Employee e1 = (Employee) context.getBean("emp1");

		System.out.println(e1);
	}

}

Step 7 : When you run the program , you can see output as below :

Happy Learning…

Leave a Reply

Your email address will not be published. Required fields are marked *