手工书写spring-beans.xml非常繁琐,而且如果多个程序员同时进行一个项目的时候,即使分成多个spring文件,也很容易会产生冲突,更别说经常遇到的错误和程序升级问题了,还好最新的Xdoclet可以帮助我们解决这个烦恼,在最新的Xdoclet中已经包含了对srping的支持,即srpingdoclet。如下是例子:
1、下载ant,从jakarta.apache.org
安装在c:\dev\ant,安装过程就不详细说了,如果ant都不懂,那看来您有些落伍了,也先别往下看了,学习一下ant先
2、下载xdoclet,当然是xdoclet.sf.net了,下载后把lib的东西放置到
c:\dev\ant\lib\xdoclet
3、java 代码
只是个例子而已
===================================================================
XdocletMain.java :
===================================================================
代码:
|
/** * Created by IntelliJ IDEA. * User: wangzy * Date: 2004-12-5 * @spring.bean * id="XdocletMain" * singleton="true" * 上述说明,这是个spring的bean,id为XdocletMain,如果看不懂,再补习一下xdoclet * @spring.property * name="bean1" * ref="Bean1" * 上述说明,有个bean1属性,关联的为Bean1的spring组件 * @spring.property * name="bean2" * ref="Bean2" * 同上 */ public class XdocletMain {
public Bean1 getBean1() { return bean1; }
public void setBean1(Bean1 bean1) { this.bean1 = bean1; }
public Bean2 getBean2() { return bean2; }
public void setBean2(Bean2 bean2) { this.bean2 = bean2; }
private Bean1 bean1 = null;
private Bean2 bean2 = null; }
|
===================================================================
Bean1.java :
===================================================================
代码:
|
/** * Created by IntelliJ IDEA. * User: wangzy * Date: 2004-12-5 * @spring.bean * id="Bean1" * singleton="true" * 声明一个Bean1的spring组件 */ public class Bean1 { }
|
===================================================================
Bean2.java :
===================================================================
代码:
|
/** * Created by IntelliJ IDEA. * User: wangzy * Date: 2004-12-5 * @spring.bean * id="Bean2" * singleton="true" * 类似Bean1 */ public class Bean2 { }
|
===================================================================
下来当然就是最重要的ant的build.xml了,其中相关部分为:
代码:
|
<!--这里是xdoclet的lib,这些都不是ant的core包,所以要引进来 --> <path id="xdoclet.class.path"> <fileset dir="c:\dev\ant\lib\xdoclet"> <include name="*.jar"/> </fileset> </path>
<!--这里就是生成spring-beans配置了 --> <target name="srpingdoclet"> <!--定义springdoclet task --> <taskdef name="springdoclet" classname="xdoclet.modules.spring.SpringDocletTask" classpathref="xdoclet.class.path" /> <!--这里根据src下的java文件中的信息,生成spring-beans.xml到“spring/spring-beans.xml”文件中--> <springdoclet destdir="spring"> <fileset dir="src" includes="**/*.java" /> <springxml destinationFile="spring-beans.xml" /> </springdoclet> </target>
|
运行ant springdoclet,看看spring/spring-beans.xml文件,大功告成!!!
具体详细信息可以看xdoclet文档
代码:
|
<?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="Bean1" class="Bean1" singleton="true" >
</bean>
<bean id="XdocletMain" class="XdocletMain" singleton="true" >
<property name="bean1"> <ref bean="Bean1"/> </property> <property name="bean2"> <ref bean="Bean2"/> </property> </bean>
<bean id="Bean2" class="Bean2" singleton="true" >
</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 <bean></bean> markup. -->
</beans>
|