Ref tag and Ref attribute Example

If there is a dependency between two objects then in the spring bean configuration file we use a tag "ref" to specify the dependencies. For example there are two objects Address and Person. Every person has an address. So we define Address object in the Person class. The way to define the dependencies between these two objects is given in the below RefTagExample.java example.
First create an Address bean under src as shown below. File: com.ram.beans.Address.java
package com.ram.beans;

public class Address {
 private String street;
 private String city;
 private String state;
 
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 
 
}

Next create a Person bean under src as shown below. File: com.ram.beans.Person.java
package com.ram.beans;

public class Person {
 private String pid;
 private String name;
 private Address address;
 
 public String getPid() {
  return pid;
 }
 public void setPid(String pid) {
  this.pid = pid;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Address getAddress() {
  return address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
 
}


Next create applicationContext.xml file under src. The file is given 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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
 <bean id="addr" class="com.ram.beans.Address">
  <property name="street" value="Richmond"></property>
  <property name="city" value="Melbourne"></property>
  <property name="state" value="Victoria"></property>
 </bean>
 
 <bean id="person" class="com.ram.beans.Person">
  <property name="pid" value="1"></property>
  <property name="name" value="Ram"></property>
  <property name="address">
   <ref bean="addr"/>
  </property>
 </bean>
 
</beans>
Next create a main class under src as shown. File: com.ram.app.RefTagExample.java
package com.ram.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ram.beans.Address;
import com.ram.beans.Person;

public class RefTagExample {
 public static void main(String[] args) {
  ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
  Person person = container.getBean("person", Person.class);
  
  System.out.println("Person Id = "+person.getPid());  
  System.out.println("Person Name = "+person.getName());
  System.out.println("Street = "+person.getAddress().getStreet());
  System.out.println("City = "+person.getAddress().getCity());
  System.out.println("State = "+person.getAddress().getState());
 }
}

Execute RefTagExample.java file. Output is given below.
Instead of "ref" tag we can use ref attribute as shown below:
<property name="address" ref="addr"><property>

No comments:

Post a Comment