Spring XDoclet

I have been hand-coding the Spring's XML application context file or known as the bean definition for quite some time. Because I am too lazy to read thru the XDoclet documentation and due to the lack of examples, especially in the @spring.bean tag section. And due to "programmer's itch" I started to tinker with it this very night. And this is what I got.

Listing 1: is a very plain sample Interface


public interface MySampleService {


public void showSpring(String greetings);

}




Listing 2: I added a simple xdoclet tag defining the bean id for Spring application context in one of the implementation classes


/**
* @author Jared Odulio
* @spring.bean
* id="sample-service"
*
*/
public class MySampleServiceImpl implements MySampleService {



public void showSpring(String greetings) {
// TODO Auto-generated method stub
if (greetings == null){
System.out.println("My Sample Service");
}else{
System.out.println(greetings);
}
}

}




Listing 3: Another sample implementation class


/**
* @author Jared Odulio
* @spring.bean
* id="another-sample"
*
*/
public class AnotherService implements MySampleService {


public void showSpring(String greetings) {
// TODO Auto-generated method stub
if (greetings == null){
System.out.println("Another Sample Service");
}else{
System.out.println(greetings);
}
}

}




Listing 3: This is our Ant build.xml file



<project name="sample-proj" default="spring" basedir=".">

<property name="conf.dir" value="${basedir}${file.separator}conf"/>
<property name="src.dir" value="${basedir}${file.separator}src"/>
<taskdef name="springdoclet" classname="xdoclet.modules.spring.SpringDocletTask"/>
<taskdef name="doclet" classname="xdoclet.DocletTask"/>
<target name="spring">
<springdoclet destdir="${conf.dir}">
<fileset dir="${src.dir}"/>
<springxml destinationfile="appcontext.xml"/>
</springdoclet>

</target>

</project>



Listing 4: When we run Ant, this what we should have


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans
default-autowire="no"
default-lazy-init="false"
default-dependency-check="none"
>

<bean
id="another-sample"
class="AnotherService"
>

</bean>

<bean
id="sample-service"
class="MySampleServiceImpl"
>

</bean>

<!--
To include additional bean definitions for Spring in the generated
application context file, add a file to your XDoclet merge directory
called spring-beans.xml that contains the markup.
-->

</beans>



There you go! Bean definition file auto-generated by XDoclet.

Take note, under normal circumstances, the build.xml file should not look as the example on Listing 3, I used Eclipse to run this, so the necessary "path" and "pathelement" tags are not present in the build.xml file because I defined all the XDoclet-related libraries in the Ant Runtime Preferences. But those tags should be defined if the build.xml has to be run outside Eclipse. I am just too lazy to do that now. And I just came from a calorie-burning, heart-pounding squash tennis game at Kallang, I should say it's BAFTB (Better and Faster Than Badminton).

Comments

Unknown said…
pareng jared,

uy, meron na pala nito... thanks for the heads up and example... even just this one and plus @spring.property ok na ok na... :)


cheers!

nox

Popular Posts