Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

References to other beans (collaborators)

text zoom
"http://www.w3.org/TR/html4/loose.dtd">







References to other beans (collaborators)


The <ref> element is the used to set the value of the specified property to be a reference to another bean. It is used inside the <constructor-arg/> or <property/> definition element. Such beans which work together with other beans are known as Collaborators. The syntax of <ref> is as follows:-


<ref bean="someBean"/> or <ref local="someBean"/>

The advantage of using local attribute give the ability of the XML parser to validate XML
id references within the same file. The XML parser will issue an error if no matching element is found in the same file.


Example ;-




package com.visualbuilder.beans;


public class WrapperBean {

    private CollectionInjectionBean collectionInjectionBean;

    private ConstructorInjectionBean constructorInjectionBean;

    private DisposableInitBean disposableInitBean;

    private SetterInjectionBean setterInjectionBean;

    private TypeCollectionInjectionBean typeCollectionInjectionBean;

    public CollectionInjectionBean getCollectionInjectionBean() {

        return collectionInjectionBean;

       
}

    public void setCollectionInjectionBean(CollectionInjectionBean collectionInjectionBean) {

               
this.collectionInjectionBean = collectionInjectionBean;

       
}

    public ConstructorInjectionBean getConstructorInjectionBean() {

               
return constructorInjectionBean;

       
}

    public void setConstructorInjectionBean(ConstructorInjectionBean constructorInjectionBean) {

               
this.constructorInjectionBean = constructorInjectionBean;

       
}

    public DisposableInitBean getDisposableInitBean() {

               
return disposableInitBean;

       
}

    public void setDisposableInitBean(DisposableInitBean disposableInitBean) {

               
this.disposableInitBean = disposableInitBean;

       
}

    public SetterInjectionBean getSetterInjectionBean() {

               
return setterInjectionBean;

       
}

    public void setSetterInjectionBean(SetterInjectionBean setterInjectionBean) {

               
this.setterInjectionBean = setterInjectionBean;

       
}

    public TypeCollectionInjectionBean getTypeCollectionInjectionBean() {

               
return typeCollectionInjectionBean;

       
}

    public void setTypeCollectionInjectionBean(TypeCollectionInjectionBean typeCollectionInjectionBean) {

               
this.typeCollectionInjectionBean = typeCollectionInjectionBean;

       
}

}





package com.visualbuilder.example;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.WrapperBean;


public class WrapperExample {

    public static void main (String [] ar){

               
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/visualbuilder/xml/spring-beans.xml"});

               
WrapperBean bean = (WrapperBean)context.getBean("wrapperBean");

        bean.getConstructorInjectionBean().printData();

       
}

}





<?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-2.5.xsd
">

   
<bean id="test" class="com.visualbuilder.beans.TestBean" lazy-init="true" />

   
<bean id="disposableBean" class="com.visualbuilder.beans.DisposableInitBean" scope="singleton" />

   
<bean id="collectionExample" class="com.visualbuilder.beans.CollectionInjectionBean" scope="singleton" lazy-init="true">

       
<property name="products">

           
<list>

               
<value>CAR</value>

               
<value>MOTORCYCLE</value>

               
<value>SCOOTER</value>

           
</list>

       
</property>

       
<property name="productSet">

           
<set>

               
<value>CAR</value>

               
<value>MOTORCYCLE</value>

               
<value>SCOOTER</value>

           
</set>

       
</property>


        <property name="prices">

           
<map>

               
<entry key="
CAR" value="100" />

               
<entry key="
MOTORCYCLE" value="200" />

               
<entry key="
SCOOTER" value="10" />

           
</map>

       
</property>

   
</bean>

   
<bean id="
typeCollectionExample" class="com.visualbuilder.beans.TypeCollectionInjectionBean" scope="singleton" lazy-init="true">

       
<property name="
products">

           
<list>

               
<value>
CAR</value>

               
<value>
MOTORCYCLE</value>

               
<value>
SCOOTER</value>

           
</list>

       
</property>

       
<property name="prices">

           
<map>

               
<entry key="CAR" value="100" />

               
<entry key="MOTORCYCLE" value="200" />

               
<entry key="SCOOTER" value="10" />

           
</map>

       
</property>

   
</bean>

   
<bean id="
constructorInjection" class="com.visualbuilder.beans.ConstructorInjectionBean" scope="singleton" lazy-init="true">

       
<constructor-arg type="
java.lang.String" value="John Smith" />

       
<constructor-arg index="
1" value="25" />

       
<constructor-arg value="
M" />

   
</bean>


    <bean id="setterInjection" class="com.visualbuilder.beans.SetterInjectionBean" sscope="singleton" lazy-init="true">

       
<property name="
name" value="John Smith" />

       
<property name="
age" value="25" />

       
<property name="
sex" value="M" />

   
</bean>

   
<bean id="
wrapperBean" class="com.visualbuilder.beans.WrapperBean" scope="singleton" lazy-init="true">

       
<property name="
collectionInjectionBean">

           
<ref bean="
collectionExample" />

       
</property>

       
<property name="
constructorInjectionBean">

           
<ref bean="
constructorInjection" />

       
</property>

       
<property name="
disposableInitBean">

           
<ref bean="
disposableBean" />

       
</property>

       
<property name="
setterInjectionBean">

           
<ref bean="
setterInjection" />

       
</property>

       
<property name="
typeCollectionInjectionBean">

           
<ref bean="
typeCollectionExample" />

       
</property>

   
</bean>       

</beans>






Output:-


Jun 2, 2008 4:31:54 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Mon Jun 02 16:31:54 IST 2008]; root of context hierarchy

Jun 2, 2008 4:31:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

afterPropertiesSet method is called

Jun 2, 2008 4:31:55 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2a5330

Jun 2, 2008 4:31:55 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2a5330: defining beans [test,disposableBean,collectionExample,typeCollectionExample,constructorInjection,setterInjection,wrapperBean]; root of factory hierarchy

Assigned Name is John Smith

Assigned Age is 25

Assigned Sex is M







                    

Copyright © 2012 VisualBuilder. All rights reserved