EJB - 无状态SessionBean简单示例
2012-09-05 10:15
260 查看
示例环境: (理论上使用最新版会比较好, 但是因为某些原因,使用如下环境)
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
注意: @Remote的注释一定要加上, 否则在后面的测试中会报出
javax.naming.NameNotFoundException: remote not bound 的错误
4. 编写传输实例类 --HelloWorlding.java
5. 编写 Session Bean --HelloWorldBean.java
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包),
直接运行就能看到效果了。
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(); } } }
直接运行就能看到效果了。
相关文章推荐
- EJB对象的部署及客户端调用简单示例
- EJB之无状态会话Bean简单应用-学习与实践(Inber原作)
- [原创]EJB循序渐进,手把手创建简单的无状态会话BEAN
- iOS 判断网络状态 简单示例
- Ejb-有状态&无状态SessionBean
- State and Effect demo,状态和效果简单示例
- EJB中JNDI的逻辑名的使用及部署_{EJB之无状态会话Bean简单应用-学习与实践}续(inber原作)
- EJB2.0有状态SessionBean
- Python实现线程状态监测简单示例
- 利用JBOSS+MyEclipse完成一个简单的EJB示例
- REST开发简单示例(jsp,jQuery,jpa,ejb,rest)
- 利用JBOSS+MyEclipse完成一个简单的EJB示例
- localStorage 简单示例
- iptables 简单配置示例 分类: Linux 2015-05-27 22:12 145人阅读 评论(0) 收藏
- Java reflect反射简单示例
- c#webservice的简单示例
- Method Overriding in Java _1(简单示例)
- 目前项目中用到的Java Web框架的MVC流程(简单示例)
- ICE简单介绍及使用示例