关于Lomboz的图解,请参考我以前的文章,这里不多说了。
1. 新建一个项目
● 右击“包资源管理器”的空白处, 在弹出菜单中选择“新建” ->“Lomboz J2EE Project”
·项目名:HelloEJB
·EJB Modules:helloEJB
·Targeted Server: JBoss 4.0.0
效果如下:(利用下图中的小倒三角按钮将引入的 JBoss 库文件过滤了 )
2. xdoclet.xml 的修改:
打开 build.xml 和 xdoclet.xml ,仔细看看里面的内容。
如果你看过关于 XDoclet 方面的文章,那么应该比较明白 Lomboz 开发 EJB 的运行方式。其实就是 Ant + XDoclet ,这里着重谈谈 xdoclet.xml 文件。
xdoclet.xml
1:
<?
xml
version="1.0"
?>
2:
3: <!-- Copyright (c) 2002,2003,2004 by eteration a.s.
4: www.eteration.com
5: All rights reserved. -->
6:
7:
8: < project name= " xdocletBuild " default= " ejbdoclet " basedir= " . " >
9:
10: <!-- Init -->
11: < target name= " init " >
12: < property file= " build.properties " />
13:
14: < property name= " ejb.dd.dir " value= " ../META-INF " />
15: < property name= " web.dd.dir " value= " ../WEB-INF " />
16: < property name= " xdoclet.force " value= " true " />
17: < property name= " container.type " value= " ejb-jar " />
18:
19: <!-- Set up java.class.path -->
20: < path id= " project.class.path " >
21: < pathelement location= " ${project.dir}/${bin.dir} " />
22:
23:
24: < fileset dir= " ${eclipse.home}/plugins " >
25: < include name= " **/ant.jar " />
26: < include name= " **/sf.net.xdoclet*/*.jar " />
27: < include name= " **/org.apache.log4j*/**/*.jar " />
28: < include name= " **/org.apache.commons*/**/*.jar " />
29: </ fileset >
30:
31: <!-- append the external classpath lastly -->
32: < pathelement path= " ${java.class.path} " />
33:
34: <!-- append the path sent through eclipse to classpath -->
35: < pathelement path= " ${project.path} " />
36:
37: </ path >
38:
39:
40: </ target >
41:
42: <!-- Run EJBDoclet -->
43: < target name= " ejbdoclet " depends= " init " >
44:
45: < taskdef name= " ejbdoclet " classname= " xdoclet.modules.ejb.EjbDocletTask " >
46: < classpath refid= " project.class.path " />
47: </ taskdef >
48:
49:
50: < ejbdoclet
51: destdir= " ${project.dir}/${ejbsrc.dir} "
52: mergedir= " ${ejb.dd.dir} "
53: excludedtags= " @version,@author,@todo "
54: addedtags= " @lomboz generated "
55: ejbspec= " 2.0 "
56: force= " ${xdoclet.force} "
57: verbose= " true " >
58:
59: <!-- PLEASE KEEP THE BEAN MARKER COMMENTS IF YOU MODIFY THE FILE.
60: CONTENTS BETWEEN THE MARKERS ARE AUTO INSERTED WITH EACH
61: BUILD. -->
62: <!-- BEANS START -->
63: < fileset dir= " ../../src " defaultexcludes= " yes " >
64: < patternset includesfile= " ejbs.xml " />
65: </ fileset >
66: <!-- BEANS END -->
67:
68: < dataobject />
69: < dao pattern= " {0} " destdir= " ${project.dir}/${ejbsrc.dir} " />
70:
71: < valueobject />
72: < utilobject cacheHomes= " true " includeGUID= " true " kind= " physical " />
73:
74: < remoteinterface />
75: < localinterface />
76: < homeinterface />
77: < localhomeinterface />
78:
79: < entitypk />
80: < entitycmp />
81: < entitybmp />
82: < session />
83:
84: < deploymentdescriptor
85: destdir= " ${ejb.dd.dir} "
86: validatexml= " false "
87: mergedir= " ${ejb.dd.dir} "
88: />
89:
90: <!--
91: Have struts form objects generated based on entity beans'
92: data objects. Will require struts.jar to compile. -->
93: < strutsform />
94:
95:
96: <!--
97: Have a mapping.xml file generated for castor classes.
98: -->
99: < castormapping destdir= " ${ejb.dd.dir} " validatexml= " false " />
100: <!-- -->
101:
102: < weblogic
103: version= " 6.1 "
104: xmlencoding= " UTF-8 "
105: destdir= " ${ejb.dd.dir} "
106: validatexml= " false "
107: datasource= " PLEASE_MODIFY_THIS "
108: mergedir= " ${ejb.dd.dir} "
109: persistence= " weblogic "
110: />
111: < jboss
112: version= " 3.0 "
113: unauthenticatedPrincipal= " nobody "
114: xmlencoding= " UTF-8 "
115: destdir= " ${ejb.dd.dir} "
116: validatexml= " false "
117: datasource= " PLEASE_MODIFY_THIS "
118: datasourcemapping= " PLEASE_MODIFY_THIS "
119: preferredrelationmapping= " PLEASE_MODIFY_THIS "
120: />
121:
122: < jrun
123: version= " 4.0 "
124: xmlencoding= " UTF-8 "
125: destdir= " ${ejb.dd.dir} "
126: validatexml= " false "
127: />
128:
129: < webSphere destdir= " ${ejb.dd.dir} " />
130: < jonas
131: version= " 3.2 "
132: xmlencoding= " UTF-8 "
133: destdir= " ${ejb.dd.dir} "
134: validatexml= " false "
135: mergedir= " ${ejb.dd.dir} "
136: />
137:
138: < orion
139: destdir= " ${ejb.dd.dir} "
140: />
141:
142: < apachesoap
143: destdir= " ${ejb.dd.dir} "
144: />
145:
146: </ ejbdoclet >
147: </ target >
148:
149: </ project >
2:
3: <!-- Copyright (c) 2002,2003,2004 by eteration a.s.
4: www.eteration.com
5: All rights reserved. -->
6:
7:
8: < project name= " xdocletBuild " default= " ejbdoclet " basedir= " . " >
9:
10: <!-- Init -->
11: < target name= " init " >
12: < property file= " build.properties " />
13:
14: < property name= " ejb.dd.dir " value= " ../META-INF " />
15: < property name= " web.dd.dir " value= " ../WEB-INF " />
16: < property name= " xdoclet.force " value= " true " />
17: < property name= " container.type " value= " ejb-jar " />
18:
19: <!-- Set up java.class.path -->
20: < path id= " project.class.path " >
21: < pathelement location= " ${project.dir}/${bin.dir} " />
22:
23:
24: < fileset dir= " ${eclipse.home}/plugins " >
25: < include name= " **/ant.jar " />
26: < include name= " **/sf.net.xdoclet*/*.jar " />
27: < include name= " **/org.apache.log4j*/**/*.jar " />
28: < include name= " **/org.apache.commons*/**/*.jar " />
29: </ fileset >
30:
31: <!-- append the external classpath lastly -->
32: < pathelement path= " ${java.class.path} " />
33:
34: <!-- append the path sent through eclipse to classpath -->
35: < pathelement path= " ${project.path} " />
36:
37: </ path >
38:
39:
40: </ target >
41:
42: <!-- Run EJBDoclet -->
43: < target name= " ejbdoclet " depends= " init " >
44:
45: < taskdef name= " ejbdoclet " classname= " xdoclet.modules.ejb.EjbDocletTask " >
46: < classpath refid= " project.class.path " />
47: </ taskdef >
48:
49:
50: < ejbdoclet
51: destdir= " ${project.dir}/${ejbsrc.dir} "
52: mergedir= " ${ejb.dd.dir} "
53: excludedtags= " @version,@author,@todo "
54: addedtags= " @lomboz generated "
55: ejbspec= " 2.0 "
56: force= " ${xdoclet.force} "
57: verbose= " true " >
58:
59: <!-- PLEASE KEEP THE BEAN MARKER COMMENTS IF YOU MODIFY THE FILE.
60: CONTENTS BETWEEN THE MARKERS ARE AUTO INSERTED WITH EACH
61: BUILD. -->
62: <!-- BEANS START -->
63: < fileset dir= " ../../src " defaultexcludes= " yes " >
64: < patternset includesfile= " ejbs.xml " />
65: </ fileset >
66: <!-- BEANS END -->
67:
68: < dataobject />
69: < dao pattern= " {0} " destdir= " ${project.dir}/${ejbsrc.dir} " />
70:
71: < valueobject />
72: < utilobject cacheHomes= " true " includeGUID= " true " kind= " physical " />
73:
74: < remoteinterface />
75: < localinterface />
76: < homeinterface />
77: < localhomeinterface />
78:
79: < entitypk />
80: < entitycmp />
81: < entitybmp />
82: < session />
83:
84: < deploymentdescriptor
85: destdir= " ${ejb.dd.dir} "
86: validatexml= " false "
87: mergedir= " ${ejb.dd.dir} "
88: />
89:
90: <!--
91: Have struts form objects generated based on entity beans'
92: data objects. Will require struts.jar to compile. -->
93: < strutsform />
94:
95:
96: <!--
97: Have a mapping.xml file generated for castor classes.
98: -->
99: < castormapping destdir= " ${ejb.dd.dir} " validatexml= " false " />
100: <!-- -->
101:
102: < weblogic
103: version= " 6.1 "
104: xmlencoding= " UTF-8 "
105: destdir= " ${ejb.dd.dir} "
106: validatexml= " false "
107: datasource= " PLEASE_MODIFY_THIS "
108: mergedir= " ${ejb.dd.dir} "
109: persistence= " weblogic "
110: />
111: < jboss
112: version= " 3.0 "
113: unauthenticatedPrincipal= " nobody "
114: xmlencoding= " UTF-8 "
115: destdir= " ${ejb.dd.dir} "
116: validatexml= " false "
117: datasource= " PLEASE_MODIFY_THIS "
118: datasourcemapping= " PLEASE_MODIFY_THIS "
119: preferredrelationmapping= " PLEASE_MODIFY_THIS "
120: />
121:
122: < jrun
123: version= " 4.0 "
124: xmlencoding= " UTF-8 "
125: destdir= " ${ejb.dd.dir} "
126: validatexml= " false "
127: />
128:
129: < webSphere destdir= " ${ejb.dd.dir} " />
130: < jonas
131: version= " 3.2 "
132: xmlencoding= " UTF-8 "
133: destdir= " ${ejb.dd.dir} "
134: validatexml= " false "
135: mergedir= " ${ejb.dd.dir} "
136: />
137:
138: < orion
139: destdir= " ${ejb.dd.dir} "
140: />
141:
142: < apachesoap
143: destdir= " ${ejb.dd.dir} "
144: />
145:
146: </ ejbdoclet >
147: </ target >
148:
149: </ project >
· 采用 links 方式安装 Lomboz 插件不能运行 XDoclet 的原因及补救方法:
24: <fileset dir="${eclipse.home}/plugins">
25: <include name="**/ant.jar"/>
26: <include name="**/sf.net.xdoclet*/*.jar"/>
27: <include name="**/org.apache.log4j*/**/*.jar"/>
28: <include name="**/org.apache.commons*/**/*.jar"/>
29: </fileset>
25: <include name="**/ant.jar"/>
26: <include name="**/sf.net.xdoclet*/*.jar"/>
27: <include name="**/org.apache.log4j*/**/*.jar"/>
28: <include name="**/org.apache.commons*/**/*.jar"/>
29: </fileset>
· 从第24行和26行可以看到,Lomboz 是从“Eclipse安装目录/plugins/”的目录下寻找 “sf.net.xdoclet_1.2.1” 子目录中的 *.jar 文件来运行 XDoclet的。 由于采用了 links 方式安装插件,所以会找不到 “sf.net.xdoclet_1.2.1” 这个目录,无法加载 XDoclet的库文件。
● 修改方法:
· 不采用 links 方式安装 Lomboz 插件;
· 若采用 links 方式安装插件,将其 Lomboz 插件目录下的那三个目录(即上面的三个目录)复制到 “Eclipse安装目录/plugins/”的目录下;
· 直接修改xdoclet.xml 文件,将“${eclipse.home}/plugins”替换成 links 方式下的插件目录;如这里,我将它替换成了<fileset dir="D:/eclipse/myplugins/lomboz-3.0.1/eclipse/plugins">
· 或
将1.2.2版本的库文件路径加入到类路径中。
注意到没有,这个版本是1.2.1,而当前版本是1.2.2;所以最好将自己下载的1.2.2版本的 Xdoclet中的库文件替换掉1.2.1版本的库文件,不然某些功能可能无法在 JBoss 4.0.0 配合使用。
● 其它修改的地方
· 看55行,生成的是2.0版本规范的EJB,当然要改成“2.1”了,人要往高处走啊。
55: ejbspec="2.0"
· 从102行到140行,可以看出 Lomboz 可以使用 weblogic 、jboss、webSphere 等服务器,由于这里我只使用 jboss开发EJB,所以只需要保留 jboss 项就够了,避免生成太多的服务器配置文件。
既然使用 jboss 了,也不是拿来就用的,修改一下,使它更适合自己。
(有关数据库的配置,下篇文章详细说明,是为了开发实体Bean设置的)
● 修改后的 XDoclet 文件
xdoclet.xml
<?xml version="1.0" encoding="GBK"?> <!-- Copyright (c) 2002, 2003,2004 Eteration Bilisim A.S. Naci Dai and others. Parts developed under contract ref:FT/R&D/MAPS/AMS/2004-09-09/AL are Copyright France Telecom, 2004. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Eteration Bilisim A.S. - initial API and implementation Naci Dai For more information on eteration, please see <http://www.eteration.com/>. --> <project name="xdocletBuild" default="ejbdoclet" basedir="."> <!-- Init --> <target name="init"> <property file="build.properties"/> <property name="ejb.dd.dir" value="../META-INF"/> <property name="web.dd.dir" value="../WEB-INF"/> <property name="xdoclet.force" value="true"/> <property name="container.type" value="ejb-jar"/> <!-- Set up java.class.path --> <path id="project.class.path"> <pathelement location="${project.dir}/${bin.dir}" /> <!-- 由于使用links方式安装Lomboz插件,所以需要修改这个目录,使其 正确指向Lomboz目录,还建议使用1.2.2版本的XDoclet 目录下的库 文件来代替 sf.net.xdoclet* 目录下的库文件 --> <fileset dir="D:/eclipse/myplugins/lomboz-3.0.1/eclipse/plugins"> <include name="**/ant.jar"/> <include name="**/sf.net.xdoclet*/*.jar"/> <include name="**/org.apache.log4j*/**/*.jar"/> <include name="**/org.apache.commons*/**/*.jar"/> </fileset> <!-- append the external classpath lastly --> <pathelement path="${java.class.path}" /> <!-- append the path sent through eclipse to classpath --> <pathelement path="${project.path}" /> </path> </target> <!-- Run EJBDoclet --> <target name="ejbdoclet" depends="init"> <taskdef name="ejbdoclet" classname="xdoclet.modules.ejb.EjbDocletTask"> <classpath refid="project.class.path"/> </taskdef> <ejbdoclet destdir="${project.dir}/${ejbsrc.dir}" mergedir="${ejb.dd.dir}" excludedtags="@version,@author,@todo" addedtags="@lomboz generated" ejbspec="2.1" force="${xdoclet.force}" verbose="true" > <!-- PLEASE KEEP THE BEAN MARKER COMMENTS IF YOU MODIFY THE FILE. CONTENTS BETWEEN THE MARKERS ARE AUTO INSERTED WITH EACH BUILD. --> <!-- BEANS START --> <fileset dir="../../src" defaultexcludes="yes"> <patternset includesfile="ejbs.xml" /> </fileset> <!-- BEANS END --> <dataobject/> <dao pattern="{0}" destdir="${project.dir}/${ejbsrc.dir}"/> <valueobject/> <utilobject cacheHomes="true" includeGUID="true" kind="physical"/> <remoteinterface/> <localinterface/> <homeinterface /> <localhomeinterface/> <entitypk/> <entitycmp/> <entitybmp/> <session/> <deploymentdescriptor
destdir="${ejb.dd.dir}"
validatexml="false" mergedir="${ejb.dd.dir}"
/> <!-- Have struts form objects generated based on entity beans' data objects. Will require struts.jar to compile. --> <strutsform /> <!-- Have a mapping.xml file generated for castor classes. --> <castormapping destdir="${ejb.dd.dir}" validatexml="false" /> <!-- --> <jboss version="4.0" unauthenticatedPrincipal="nobody" xmlencoding="GBK" destdir="${ejb.dd.dir}" validatexml="false" datasource="java:/MySqlDS" datasourcemapping="mySQL" preferredrelationmapping="foreign-key" /> <apachesoap
destdir="${ejb.dd.dir}" /> </ejbdoclet> </target> </project>
小技巧:
以后我都会用这个文件来执行 XDoclet, 如果每次新建一个项目都修改它的话,有些麻烦。这里有一个方法,找到
Lomboz 插件目录下的“ com.objectlearn.jdt.j2ee_3.0.1”子目录,发现有个“lomboz.jar”文件,解压这个文件,在解压后的文件中的“com/objectlearn/jdt/j2ee/templates/ejb” 目录有个“xdoclet.xml”,打开它看看,是不是和项目中的 xdoclet.xml 一样。你可以将修改后的 xdoclet.xml 替换掉这里的 xdoclet.xml,那么就不用每次都修改xdoclet.xml 了。
花了这么大的力气谈 xdoclet.xml,因为它是Lomboz开发EJB的核心,希望不会太罗嗦。
3. 编写程序
●
继续在 HelloEJB 项目中,右击“src” ->新建 ->Lomboz EJB Creation Wizard :
· 包(K) :javamxj.ejb.stateless
· 名称(M): Hello
· EJB Type: 选择 Stateless
最后点击完成。
● 在“包资源管理器”窗口中,右击 HelloBean.java ->Lomboz J2EE... ->Add EJB to Module... :勾选 helloEJB Module。
提醒
|
这时,会在 xdoclet.xml 中添加以下语句:
<fileset dir="${project.dir}/src/" defaultexcludes="yes">
<include name="javamxj/ejb/stateless/HelloBean.java" /> </fileset>
同时,在 beans.xml 中会加入
<bean>
<beanClass>javamxj.ejb.stateless.HelloBean</beanClass> <type>Session</type> </bean>
这样,就把 HelloBean.java 加入了 EJBDoclet 的任务中了。
|
● 右击 HelloBean.java ->Lomboz J2EE... ->Add EJB to Method:
· Method Signature: public String sayHello(String message)
· Method Type: Business Method
· Interface Type: Remote Interface
● 修改 HelloBean.java ,如图在45-47行添加如下语句,然后保存。
● 右击 helloEJB 模块 ->Lomboz J2EE... ->Generate EJB Classes。
看到没有?在 ejbsrc 文件夹下生成了6个文件。
4. jboss-ide 的一些技巧
其实这里没有必要生成 HelloLocal.java HelloLocalHome 这两个文件,可以通过修改ejb.bean标记来除去这两个文件。
JBoss-IDE 现在已经改名 JBoss Eclipse IDE 了,为了方便,我在下文中还是称为 JBoss-IDE。
关于 JBoss-IDE 的详细用法,请参考其主页上的教程。这里简单说一些技巧。
· 切换倒主窗口:
· 向下拉动滚动条,选择“view-type”这个属性,然后点击,如下图:
这里选择点击“remote”。
这样 view-type = "remote" 这个语句就输入了,方便吧!主要免去记忆和查找之苦。
· 保存,然后删除 ejbsrc 这个文件夹,再次 Generate EJB Classes,这么样,这时会发现生成的 ejbsrc文件夹下只有4个文件了吧。
· 本来没有必要删除那两个文件的,插上这么一段主要是为了介绍使用 JBoss-IDE 的方便之处。其实,JBoss-IDE 的好处还很多,自己慢慢体会吧。
提醒
|
打开JBoss-IDE目录下的子目录 D:/eclipse/myplugins/jboss-ide-1.40/eclipse/plugins/org.jboss.ide.eclipse.xdoclet.core_1.4.0 (我采用的 links 方式安装),发现其中的XDoclet库文件是1.2.1版本,最好用1.2.2版本下对应的库文件将其替换掉,其它文件不要乱动。
另外,还可以添加其它如 IBM、BEA 的库文件到这个目录,使 JBoss-IDE 不仅仅支持 JBoss,还可以支持其它服务器,甚至自己编写库文件也可以加入。
还有,至于“Alt+/”是干什么的,可以查看: Eclipse的主窗口 ->帮助 ->提示与技巧 ->Eclipse Java 开发工具,很不错的。
|
5. 运行服务器
废话也说的差不多了,再次进入正题。
· 先启动服务器
如图,有多种方法可以启动JBoss服务器。其它操作也是一样,最好自己多练习,多用右键。
· 然后右击 helloEJB,在弹出菜单中选择部署模块。如果一切正常,控制台会输出类似语句:
21:51:46,203 INFO [EjbModule] Deploying Hello
21:51:47,656 INFO [EJBDeployer] Deployed: file:/F:/java/jboss/server/default/deploy/helloEJB.jar
21:51:47,656 INFO [EJBDeployer] Deployed: file:/F:/java/jboss/server/default/deploy/helloEJB.jar
好了,服务端已经就绪了。
6.开发客户端:
· 右击 src 文件夹 ->新建 ->Lomboz EJB Test Client Wizard:
其中“包”和“名称”要自己手工输入,“EJB Home” 和 “EJB Interface” 可以通过选择输入。
· 这时会生成一个“HelloClient.java”的文件,打开它,如图添加两条语句。
· 保存,然后运行 HelloClient.java,可以在控制台看到输出结果:
·此时,服务端输出:
22:25:45,156 INFO [STDOUT] 我在EJB的服务器端,客户端正在调用'sayHello'方法。
22:25:45,156 INFO [STDOUT] Hello javamxj(CSDN)
22:25:45,156 INFO [STDOUT] Hello javamxj(CSDN)
好了,总算写完了,够累的。
这只是一个开头,后面文章主要会谈谈如何开发实体EJB,到时候关于界面及配置的就不多谈了,会把重点放在开发上。
工欲善其事,必先利其器。
你的工具配备好了吗?