Java: Bean Descriptor XML for Spring Framework

Posted on May 19, 2009

0


It is necessary for each controller (read as – business service) to be declared as a bean in Spring Framework. Referring to How to create a bean descriptor XML file?, these are the ways to register a controller (read as – service) as a bean:

Case 1. The XML Bean Declaration File:

<beans>
<bean id="helloController" class="org.xandertan.spring.HelloController">
<property name="aString"><value>Hello</value></property>
<property name="anInteger"><value>1</value></property>
</bean>
</beans>

Controller Class:

package org.xandertan.spring;
public class HelloController implements Controller {
private String s;
private int i;

public void setAString(String s) { this.s = s; }
public void setAnInteger(int i) { this.i = i; }
}

Case 2. The XML Bean Declaration File:

<beans>
<bean id="helloController" class="org.xandertan.spring.HelloController">
<property name="beanOne"><ref bean="anotherBean" /></property>
<property name="beanTwo"><ref bean="yetAnotherBean" /></property>
</bean>
</beans>

<bean id="anotherBean" class="org.xandertan.core.AnotherBean" />
<bean id="yetAnotherBean" class="org.xandertan.core.YetAnotherBean" />

Controller Class:

package org.xandertan.spring;

import org.xandertan.core.*;

public class HelloController implements Controller {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;

public void setBeanOne(AnotherBean beanOne) { this.beanOne = beanOne; }
public void setBeanTwo(YetAnotherBean beanTwo) { this.beanTwo = beanTwo; }
}

Case 3. The XML Bean Declaration File:

<beans>
<bean id="helloController" class="org.xandertan.spring.HelloController">
<constructor-arg><ref bean="anotherBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>
</beans>

<bean id="anotherBean" class="org.xandertan.core.AnotherBean" />
<bean id="yetAnotherBean" class="org.xandertan.core.YetAnotherBean" />

Controller Class:

package org.xandertan.spring;

import org.xandertan.core.*;

public class HelloController implements Controller {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;

public HelloController(AnotherBean b1, YetAnotherBean b2, int i) {
this.beanOne = b1;
this.beanTwo = b2;
this.i = i;
}
}
About these ads
Posted in: Technology