EJB - 无状态SessionBean简单示例

示例环境: (理论上使用最新版会比较好, 但是因为某些原因,使用如下环境)

1. jdk1.5.0_14

2. Eclipse 3.7

3. jboss-4.2.2.GA


步骤和代码

1. 新建Project(EJB Project 和Java Project都可以) -- ejbTest

2.  导入jboss的lib 包。 -- 右键点击工程->属性-->Java Build Path--> Add External JARs, 把Jboss的client目录下的jar文件都包进去。

3.  新建远程接口 - HelloWorld.java

/**
 * @Title:HelloWorld.java
 * @package: service
 * @Description: 
 * @author: oscar999
 * @date: 2012-9-5
 * @version V1.0
 */
package service;

import java.util.List;

import javax.ejb.Remote;

import entity.HelloWorlding;

@Remote
public interface HelloWorld {
	public String hello(String message);

	public List<HelloWorlding> getAllHelloWorlding();
}

注意: @Remote的注释一定要加上, 否则在后面的测试中会报出

javax.naming.NameNotFoundException: remote not bound   的错误


4. 编写传输实例类 --HelloWorlding.java

/**
 * @Title:HelloWorlding.java
 * @package: entity
 * @Description: 
 * @author: oscar999
 * @date: 2012-9-5
 * @version V1.0
 */
package entity;

import java.io.Serializable;

public class HelloWorlding implements Serializable {

	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

5.  编写 Session Bean --HelloWorldBean.java

/**
 * @Title:HelloWorldBean.java
 * @package: service
 * @Description: 
 * @author: oscar999
 * @date: 2012-9-5
 * @version V1.0
 */
package service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import entity.HelloWorlding;

@Stateless
public class HelloWorldBean implements HelloWorld {

	public String hello(String message) {
		return "Hello World " + message;
	}

	public List<HelloWorlding> getAllHelloWorlding() {
		List<HelloWorlding> helloworldings = new ArrayList<HelloWorlding>();
		HelloWorlding helloworlding = new HelloWorlding();
		helloworlding.setId(10);
		helloworlding.setName("oscar999");
		helloworldings.add(helloworlding);
		helloworlding = new HelloWorlding();
		helloworlding.setId(11);
		helloworlding.setName("silver");
		helloworldings.add(helloworlding);
		return helloworldings;
	}

}

6.  打包,部署

右键点击Project-->Export, 产生 ejbTest.jar 文件, 结构如下(如果创建的是EJB Project,会自动产生META-INF这个文件夹和里面的文件,)

entity

   --  HelloWorlding.class

service

   -- HelloWorld.class

   -- HelloWorldBean.class

META-INF

  -- NABUFEST.MF


把ejbTest.jar 直接复制到jboss的server\default\deploy  目录下

至此,服务端的工作就结束了, 接下来就可以在测试端测试了。

在原工程或是新建工程建立测试类(新建的话需要导入相应的lib包),

/**
 * @Title:TestClient.java
 * @package: client
 * @Description: 
 * @author: oscar999
 * @date: 2012-9-5
 * @version V1.0
 */
package client;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import service.HelloWorld;

public class TestClient {

	public static void main(String[] args) {
		try {
			Properties prop = new Properties();
			prop.setProperty(Context.PROVIDER_URL, "localhost:1099");
			prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			InitialContext ctx = new InitialContext(prop);
			HelloWorld helloworld = (HelloWorld) ctx
					.lookup("HelloWorldBean/remote");
			System.out.println(helloworld.hello("Oscar"));
			System.out.println(helloworld.getAllHelloWorlding().get(0).getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

直接运行就能看到效果了。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oscar999

送以玫瑰,手留余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值