在第二部分"使用 Eclipse 开发 Web 应用"中,我们已经在 TestJ2EE 这个 J2EE 项目中添加了一个名为 MyEJB 的 EJB 模块。下面我们通过 Lomboz EJB Creation Wizard 向 MyEJB 模块中添加一个 Session Bean。
点击 File->New->Lomboz EJB Creation Wizard,或者右击 MyEJB 模块,选择 New->Lomboz EJB Creation Wizard,显示出如下对话框:
在 com.dino.testj2ee.myejb 这个包下面创建一个名为 MyEJB 的 Stateless Session Bean。完成以后,在 TestJ2EE 项目的 src 文件夹下面,出现了一个名为 MyEJBBean.java 的文件,右击这个文件,选择 New->Lomboz EJB Method Wizard,可以给这个 EJB 添加各种方法。我添加了一个 helloWorld 方法,返回一个 String 类型。如下图所示:
然后,需要把 MyEJB 这个 Session Bean 添加到 MyEJB 这个模块上面:右击 MyEJB.java,选择 Lomboz J2EE … ->Add EJB to Module,选择 MyEJB,OK。
右击 MyEJB 模块,选择 LombozJ2EE … ->Generate EJB Classes,这样就在 ejbsrc 目录下面自动生成了下面几个文件:
MyEJBHome.java
MyEJBRome.java
MyEJBLocalHome.java
MyEJBLocal.java
MyEJBSession.java (MyEJBSession extends MyEJBBean)
MyEJBUtil.java
Build project 以后,自动将模块打包完毕。接着就可以按照部署 Web 模块的步骤来部署 EJB 模块了。
最后,我们建立一个应用程序来测试 EJB。
通过 File->New->Lomboz EJB Test Client Wizard 打开一个对话框,按下图填写:
完成以后,在 src 目录下面生成了 TestMyEJB.java,在其中的 testBean 方法中加入测试代码:
System.out.println(myBean.helloWorld()); |
如果目前 JBoss 服务器已经启动,而且 MyEJB 模块已经部署的话,执行 TestMyEJB,就会在 Console 窗口上显示出:
hehe, I can deploy EJB by Eclipse! |
四、总结
通过以上的介绍,我们了解到,Lomboz 使 Eclipse 开发 J2EE 应用变得非常简便,而且 Eclipse, Lomboz, JBoss 这些工具都是免费和开放源码的项目,所以这样的组合对 Java 开发者来说更有吸引力。
对于开发复杂的 J2EE 应用来说,上面的介绍可能还不够全面,希望大家在做项目的过程中体会,再将总结出来的经验放到 developerWorks 上来分享。
附录:JBoss320.server
<serverDefinition name="JBOSS 3.2" ejbModules="true" webModules="true" earModules="true"> <property id="serverRootDirectory" label="Application Server Directory:" type="directory" default="D:/jboss-3.2.0" /> <property id="serverAddress" label="Address:" type="string" default="127.0.0.1" /> <property id="serverPort" label="Port:" type="string" default="8080" /> <property id="classPathVariableName" label="Classpath Variable Name:" type="string" default="JBOSS32" /> <property id="classPath" label="Classpath Variable:" type="directory" default="D:/jboss-3.2.0" /> <serverHome>${serverRootDirectory}</serverHome> <webModulesDeployDirectory>${serverRootDirectory}/server/all/deploy </webModulesDeployDirectory> <ejbModulesDeployDirectory>${serverRootDirectory}/server/all/deploy </ejbModulesDeployDirectory> <earModulesDeployDirectory>${serverRootDirectory}/server/all/deploy </earModulesDeployDirectory> <jndiInitialContextFactory>org.jnp.interfaces.NamingContextFactory </jndiInitialContextFactory> <jndiProviderUrl>jnp://${serverAddress}:1099</jndiProviderUrl> <startClass>org.jboss.Main</startClass> <startWorkingDirectory>${serverRootDirectory}/bin </startWorkingDirectory> <startVmParameters></startVmParameters> <startProgramArguments>-c all</startProgramArguments> <stopClass>org.jboss.Shutdown</stopClass> <stopWorkingDirectory>${serverRootDirectory}/bin </stopWorkingDirectory> <stopVmParameters></stopVmParameters> <stopProgramArguments>${serverAddress} ${serverPort} </stopProgramArguments> <serverClassPath> <jar type="variable">JDK_TOOLS</jar> <jar type="variable">${classPathVariableName}/bin/run.jar </jar> <jar type="variable">${classPathVariableName}/bin/shutdown.jar </jar> <jar type="variable">${classPathVariableName}/client/jboss-j2ee.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/javax.servlet.jar </jar> <jar type="variable">${classPathVariableName}/lib/commons-httpclient.jar </jar> <jar type="variable">${classPathVariableName}/lib/concurrent.jar </jar> <jar type="variable">${classPathVariableName}/lib/getopt.jar </jar> <jar type="variable">${classPathVariableName}/lib/jboss-boot.jar </jar> <jar type="variable">${classPathVariableName}/lib/jboss-common.jar </jar> <jar type="variable">${classPathVariableName}/lib/jboss-jmx.jar </jar> <jar type="variable">${classPathVariableName}/lib/jboss-system.jar </jar> <jar type="variable">${classPathVariableName}/lib/jdom.jar </jar> <jar type="variable">${classPathVariableName}/lib/log4j-boot.jar </jar> <jar type="variable">${classPathVariableName}/lib/webdavlib.jar </jar> <jar type="variable">${classPathVariableName}/lib/xercesImpl.jar </jar> <jar type="variable">${classPathVariableName}/lib/xml-apis.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jnet.jar </jar> <jar type="variable">${classPathVariableName}/lib/gnu-regexp.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jboss.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jnpserver.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jpl-util.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jpl-pattern.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jsse.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jbossha.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jboss-jaas.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jts.jar </jar> <jar type="variable"> ${classPathVariableName}/server/all/lib/jboss-transaction.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/javagroups-2.0.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jmxri.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jmxtools.jar </jar> <jar type="variable">${classPathVariableName}/server/all/lib/jbossmx.jar </jar> </serverClassPath> <clientClassPath> <jar type="variable">${classPathVariableName}/client/jboss-j2ee.jar </jar> <jar type="variable">${classPathVariableName}/client/jnp-client.jar </jar> <jar type="variable">${classPathVariableName}/client/jboss-client.jar </jar> <jar type="variable">${classPathVariableName}/client/jboss-common-client.jar </jar> <jar type="variable">${classPathVariableName}/client/jbosssx-client.jar </jar> <jar type="variable">${classPathVariableName}/client/log4j.jar </jar> <jar type="variable">${classPathVariableName}/client/jnet.jar </jar> <jar type="variable">${classPathVariableName}/client/jaas.jar </jar> </clientClassPath> </serverDefinition> |
参考资料