---
title: "ejb3.0读书笔记一"
author: "cainwise"
date: "2008-09-14T02:01:46Z"
tags: []
slug: "ejb3-0"
draft: false
lastmod: "2008-09-14T02:01:46Z"
id: 241536
url: "https://cainwise.iteye.com/blog/241536"
---

我的配置环境是myeclipse6.0+jboss4.2.5+eclipse3.3.2

 

 

1.首先在myeclipse新建一个ejb3.0的项目

 新建接口HelloWord.java

```
package com.foshanshop.ejb3;

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

 然后建立实现这个接口的实现类HelloWorldBean.java

 

  

```
package com.foshanshop.ejb3;

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



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

	public String say(String name) {
		return name+"你好啊！！";
	}

}
```

 

这样一个无状态的session bean就这样建立了

在这里我们要对这个进行测试

1.必须加入jbossall-client.jar这个文件

2.启动jboss服务器

3.把新建的ejb项目加载到服务器哪里

4.因为我是在junit上面测试的所以还要加入junit-4.0.jar

5.在你的项目中右键new----&gt;Source Folder建立一个Test文件夹

6.建立测试文件

 

    

```
package com;

import java.util.Properties;

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

import com.foshanshop.ejb3.HelloWorld;

import junit.framework.TestCase;

public class TestDemo extends TestCase {
	
	public void testMM(){
			Properties props = new Properties();
		
		try{
	        props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
	        props.setProperty("java.naming.provider.url", "localhost:1099");
	        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
	        Context ctx = new InitialContext(props); 
	        
	        System.out.println("ffffffffffffff"+ctx );
	        HelloWorld hello=(HelloWorld)ctx.lookup("HelloWorldBean/remote");
			System.out.println(hello.say("t_one"));
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
```

 运行结果：

              [ffffffffffffffjavax.naming.InitialContext@171bbc9](mailto:ffffffffffffffjavax.naming.InitialContext@171bbc9)  
              t\_one你好啊！！