Spring Tutorial Home

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

Spring Home

Java Resources

Community

Site

How to create a bean descriptor XML file?

text zoom

This section will mention to the ways to declare a business service as a bean


Example for registration a service as bean


<beans>
<bean id="greetingService" class="com.visualbuilder.spring.GreetingService"/>
</beans>


In this example,class “com.visualbuilder.spring.GreetingService” is registered in Spring under name "greetingService".


Example for passing values into properties when initialize bean
Service class:


package com.visualbuilder.spring;
public class ExampleBean
{
 private String s;
 private int i;


 public void setStringProperty(String s) { this.s = s; }
 public void setIntegerProperty(int i) { this.i = i; }
}


Bean declaration:


<bean id="exampleBean" class="com.visualbuilder.spring.ExampleBean">
 <property name="stringProperty"><value>Hi!</value></property>
 <property name="integerProperty"><value>1</value></property>
</bean>

Example for passing property value as an another bean
Service class:


package com.visualbuilder.spring;
public class ExampleBean
{
 private AnotherBean beanOne;
 private YetAnotherBean beanTwo;
 
 public void setBeanOne(AnotherBean b) { beanOne = b; }
 public void setBeanTwo(YetAnotherBean b) { beanTwo = b; }
}


In the XML bean descriptor file:

<bean id="exampleBean" class="com.visualbuilder.spring.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnotherBean"/></property>
</bean>


<bean id="anotherExampleBean" class="eg.AnotherBean"/>
<bean id="yetAnotherBean" class="eg.YetAnotherBean"/>



Example for passing parameter value into constructor:
Service class:

package com.visualbuilder.spring;
public class ExampleBean
{
 private AnotherBean beanOne;
 private YetAnotherBean beanTwo;
 private int i;


 public ExampleBean(AnotherBean b1,YetAnotherBean b2,int i)
 {
  this.beanOne = b1;
  this.beanTwo = b2;
  this.i = i;
 }
}



In the XML bean descriptor file:

<bean id="exampleBean" class="com.visualbuilder.spring.ExampleBean">
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>


<bean id="anotherExampleBean" class="com.visualbuilder.spring.AnotherBean"/>


<bean id="yetAnotherBean" class="com.visualbuilder.spring.YetAnotherBean"/>







                    

Copyright © 2014 VisualBuilder. All rights reserved