EJB Dev 1: EJB入门例子

本文详细介绍EJB的概念及其实现方法,通过具体实例演示如何使用JBoss部署无状态Session Bean,并结合Ant进行自动化构建与发布。此外,还介绍了客户端如何通过JNDI调用EJB服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字:EJB ANT JBoss 入门实例

说明:本文从一个实例说明EJB的基本理念

1. 简单说一下EJB概念

引用几句比较官方的话来定义EJB

    "EJB(Enterprise JavaBeans) is a server-side component framework that simplifies the process of building enterprise-class distributed component applications in java. By using EJB, you can write scalable, reliable, secure applications without writing your own complex distibuted component framework."  

这句话主要说明EJB是服务器端的组件框架,用来构建分布式应用;

 

    "EJB is a standard for developing and deploying server-side distributed components in Java. It defines an agreement (contract) between components and application servers that enables any component to run in any compliant application server."

这句话定义了EJB。

 

看一个图说明EJB的分类



 2. EJB运行环境JBoss安装

下载4.2版本以后,我这里就用4.2.1因为工作中用的就是这个版本,下载链接http://www.jboss.org/jbossas/downloads/

下载完解压,我将其解压到C盘根目录,解压完成配置JBoss Home,如下图:



 单机C:\jboss-4.2.1.GA\bin下run.bat可以启动JBoss,如果启动过程不抛出异常,说明JBoss启动正常。

 

3.安装Ant

下载http://ant.apache.org/下载完我同样解压到C盘根目录,配置Ant Home,如下图



 将路径C:\apache-ant-1.8.1\bin添加到系统path.

 

4. 开发只实现Remote接口的无状态Session Bean

Step1: Eclipse下新建一个JavaProject com.ejb.helloworld;

Step2: 定义一个包括业务方法的接口。调用EJB的客户端只需要使用这个接口变可以调用服务器内部这个接口的实现,接口代码:

 

package com.ejb.helloworld;

public interface HelloWorld {
	public StringsayHello(String name);
}

 Step3:编写Bean Class,此类是上述接口在服务器端的实现,代码

 

package com.ejb.helloworld.impl;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.ejb.helloworld.HelloWorld;

@Stateless
@Remote ({HelloWorld.class})
public class HelloWorldBean implements HelloWorld {

	public StringsayHello(String name) {
		 return name + " saying Hello World Loudly...";   
	}
}

 注意加粗两行代表该Bean定义EJB规范。

 Step4:Ant将上述工程打包,发布到JBoss。

在com.ejb.helloworld根目录下构建build.xml,XML内容:

<?xml version="1.0"?>

<project name="com.ejb.helloworld" default="deploy" basedir="..">

	<property environment="env" />
	<property name="app.dir" value="${basedir}\com.ejb.helloworld" />
	<property name="src.dir" value="${app.dir}\src" />
	<property name="jboss.home" value="${env.JBOSS_HOME}" />
	<property name="jboss.server.config" value="default" />
	<property name="build.dir" value="${app.dir}\build" />
	<property name="build.classes.dir" value="${build.dir}\classes" />

	<path id="build.classpath">
		<fileset dir="${jboss.home}\client">
			<include name="*.jar" />			
		</fileset>
		<pathelement location="${build.classes.dir}" />
	</path>

	<target name="prepare" depends="clean">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.classes.dir}" />
	</target>

	<target name="compile" depends="prepare" description="compile">
		<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**">
			<classpath refid="build.classpath" />
		</javac>
	</target>

	<target name="ejbjar" depends="compile" description="caeate hello world jar file...">
		<jar jarfile="${app.dir}\HelloWorld.jar">
			<fileset dir="${build.classes.dir}">
				<include name="com/**/*.class" />
			</fileset>
		</jar>
	</target>

	<target name="deploy" depends="ejbjar">
		<copy file="${app.dir}\HelloWorld.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
	</target>

	<target name="clean">
		<delete dir="${build.dir}" />
		<delete file="${jboss.home}\server\${jboss.server.config}\deploy\HelloWorld.jar" />
	</target>

</project>

 

关于如何使用Ant这里不多说,Build成功后在JBoss的Console中会有相应提示,如下图:



 上图说明com.ejb.helloworldbuild成功是jar包已经部署到JBoss,并且部署成功,当然可以到C:\jboss-4.2.1.GA\server\default\deploy目录下查看,

另外还可以到http://127.0.0.1:8080/jmx-console/HtmlAdaptor处查看

在打开页面中



 单机service=JNDIView查看JNDI名称

在出现的页面中找到“List of Mbean opreations”点击invoke按钮



 在弹出页面Global JNDI Namespace最下端会出现HelloWorld.jar的JNDI信息



 5.客户端开发

客户端为简单的Web界面,就一个jsp文件

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="com.ejb.helloworld.HelloWorld, javax.naming.*, java.util.Properties"%>
<%
		Properties props = new Properties();
		props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		props.setProperty("java.naming.provider.url", "localhost:1099");
		try {
			InitialContext ctx = new InitialContext(props);
			HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
			out.println(helloworld.sayHello("Kobe bryant"));
		} catch (NamingException e) {
			out.println(e.getMessage());
		}
%>

 

同样build次项目

build.xml内容如下

<?xml version="1.0"?>

<project name="EJBTest" default="deploy" basedir=".">

	<property environment="env" />
	<property name="resouce.dir" value="${basedir}\web" />
	<property name="jboss.home" value="${env.JBOSS_HOME}" />
	<property name="jboss.server.config" value="default" />

	<target name="web-war" description="careate web war" depends="clean">
	    <war warfile="${basedir}\hello.war" webxml="${resouce.dir}\WEB-INF\web.xml">
	      <fileset dir="${resouce.dir}">
	        <include name="**/*"/>
	      </fileset>
	    </war>
	</target>

	<target name="deploy" depends="web-war">
		<copy file="${basedir}\hello.war" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
	</target>

	<target name="clean">
		<delete file="${jboss.home}\server\${jboss.server.config}\deploy\hello.war" />
	</target>

</project>

 

6。测试

http://127.0.0.1:8080/hello/hello.jsp会出现页面输出一行字符

Kobe bryant saying Hello World Loudly... 

 7.总结




 
 本例子主要流程:

1 客户端(Web Client)请求hello.jsp文件

2 应用服务器引擎编译hello.jsp

3 hello.jsp通过JNDI查找HelloWorldBean的EJB存根,调运sayHello方法

4 将结果返回客户

 

8。由于时间关系,本例子写的比较粗,如果有人提出不同意见欢迎。项目在附件dev-workspace.rar

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值