---
title: "Stateless Session Beans（无状态bean）开发"
author: "gdf84"
date: "2008-07-28T17:00:23Z"
tags: []
categories: ["企业架构"]
slug: "stateless-session-beans-bean"
draft: false
lastmod: "2008-07-28T17:00:23Z"
id: 220590
url: "https://gdf84.iteye.com/blog/220590"
---

**1，Stateless Session Beans(无状态bean)开发**

1，1开发只存在Remote接口的无状态Session Bean

步骤：

1） 要定义一个会话bean，首先必须定义一个包含他所有业务方法的接口，这个接口不需要任何注释，他的定义方法与普通的的接口定义没什么区别，接口定义如下：

HelloWorld接口

```
package com.gdf.ejb;

public interface HelloWorld {
	
	public String sayHello(String name);
}
```

 2）实现上面的接口，并加入两个注释@Stateless，@Remote。第一个注释是无状态会话bean，第二个注释是声明访问EJB的方式。引入相关的jar包。

```
package com.gdf.ejb;

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

@Stateless
@Remote
public class HelloWorldBean implements HelloWorld {

	public String sayHello(String name) {
		return name+"说：你好！这是我的第一个EJB程序！";
	}

	
}
```

 3） 开发客户端，新建一个普通的Java项目，加入client相关的jar包，并在src目录下建立文件jndi.properties，引入jar包的路径为JBOSS\_HOME/client/\*.jar。编写一个带有main()方法的类HelloWorldClient.java。代码分别如下：

jndi.properties

```
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming\:org.jnp.interfaces
java.naming.provider.url=localhost
```

 HelloWorldClient:

```
package com.gdf.ejb;

import javax.naming.InitialContext;

public class HelloWorldClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		InitialContext ctx = new InitialContext();
		HelloWorld hw = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
		String s =  hw.sayHello("张三");
		System.out.println(s);
		
	}

}
```

 

 运行程序得到如下结果 ：张三说：你好！这是我的第一个EJB程序！