
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"/>
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin




