Home » Spring » Spring Bean Reference ,<ref> Tag in Spring

Spring Bean Reference ,<ref> Tag in Spring

In Spring if one Bean depends on another Bean class for some business logic, then this type of dependency is called object dependency.In case of Object dependency in Spring, the spring IOC container is responsible for creating that required object and injecting into the dependent classes.

In Spring we need to use <ref> element to inform spring container about the object dependency.

In Spring, beans can "access" to each other by specify the bean references in the same or different bean configuration file.In spring we can write multiple configuration xml file.Our associated bean may be in same xml or in other xml file.

Beans defined in different XML files

If you are trying to access bean from different XML file, then we need to use ‘ref‘ tag with ‘bean‘ attribute.

Syntax :

<ref bean="someBean"/>

Consider the xml file given below.

account-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="accountDetails"    class="com.jwt.spring.AccountImpl"/>   
</beans>

In the above xml file AccountImpl bean is configured.

Now in the below xml file we are referring ,”accountDetails” configured in the account=bean.xml file, so we need to use ‘ref‘ tag with ‘bean‘ attribute i.e <ref bean=”accountDetails”/>.

bank-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="bankservice" class="com.jwt.spring.BankService">
      <property name="accdetails">
        <ref bean="accountDetails"/>
      </property>
    </bean>
</beans>

Bean defined in same XML file

If you are referring to a bean in same XML file, you can reference it with ‘ref‘ tag, and ‘local‘ attribute.

Syntax :

<ref local=”referencebean”/>

In the below xml file,”accountDetails” is configured in the same beans.xml so we need to use ‘ref‘ tag with ‘local‘ attribute.

Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="accountDetails" class="com.jwt.spring.AccountImpl"/>
    <bean id="bankservice" class="com.jwt.spring.BankService">
      <property name="accdetails">
        <ref local="accountDetails"/>
      </property>
    </bean> 
</beans>

In next article we will use this concept in a sample program which will clear all your doubts , let us see the program on this concept in the next article Spring Bean reference Example


Previous Next Article
comments powered by Disqus