Study EJB (1)
来源:互联网 发布:通达信 数据引用公式 编辑:程序博客网 时间:2024/06/16 04:13
Study EJB 1
刚开始学习EJB , 这些例子是从<java2 技术内幕中扒下来>中扒下来。
以一个例子为开始:HelloWorld
写一个EJB的步骤:
1. 声明远程接口
public interface HelloWorld extends EJBObject
{
public String sayHello(String words) throws RemoteException;
}
2. 添加工厂
2.public interface HelloWorldHome extends EJBHome
{
public HelloWorld create() throws CreateExceptio , RemoteException;
}
3. 实现bean
public class HelloWorldBean implements SessionBean
{
//接口中的方法
public void ejbActivate(){}
public void ejbRemove() {}
public void ejbPassivate() {}
public void setSessionContext( SessionContext ctx) {}
public void ejbCreate() throws CreateException {}
//以下是要完成接口HelloWorld 中的业务方法
public String sayHello(String words) throws RemoteException
{
System.out.println(“liuyd say to everybody”+words);
return words;
}
}
4. 对类进行编译
5. 编写 EJB 部署说明文件
<?xml version="1.0" ?>
<!DOCTYPE ejb-jar (View Source for full doctype...)>
- <ejb-jar>
- <enterprise-beans>
- <session>
<ejb-name>HelloWorld</ejb-name>
<home> HelloWorldHome</home>
<remote> HelloWorld</remote>
<ejb-class> HelloWorldBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
6. 将Bean打包(包括所有类、部署文件)
7. 产生EJB容器代码(EJB容器自己完成对文件在EJB中的部署)
8. 部署EJB
9. 编写客户端
public class SimpleEJBClient
{
private static final String JNDI_NAME = "HelloWorld";
public static void main(String[] args)
throws Exception
{
try
{
//jndi properties
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL,
"t3://localhost:7001");
//lookup jndi initial context
InitialContext ctx = new InitialContext(h);
//lookup home
Object obj = ctx.lookup(JNDI_NAME);
//cast home
HelloWorldHome home =(HelloWorldHome)PortableRemoteObject.narrow(obj, elloWorldHome.class);
//create the bean
Object ejbObj = home.create();
//cast the bean to our interface
HelloWorld simpleEJB = (HelloWorld)PortableRemoteObject.narrow(ejbObj, HelloWorld.class);
/call bean's method
System.out.println("Calling Simple EJB: " + simpleEJB.sayIt());
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
- Study EJB (1)
- EJB study
- Study EJB (2)
- EJB's study
- study-1
- study notes for EJB 3 in action (I)
- study notes for EJB 3 in action (II)
- study notes for EJB 3 in action (III)
- MySql study notes [1]
- VBA Study(1)
- Log4j Study (1)
- Lucene Study(1)
- SWFT study-1
- D3D study 1
- Portlet Study Notes (1)
- stl study 1
- OpenGL Study 1
- awk study(1)
- 个性书签
- 精妙SQL语句
- 数组学习(续一)
- 今天计算定标问题
- symbian应用程序类型介绍
- Study EJB (1)
- microsoft WINDOWS 系统错误代码
- [翻译]Ajax 陷阱(Ajax Gotchas)
- Struts中Tiles标签简介
- C++中的函数重载
- 在Delphi中使用SQL语句插入包括时间日期的记录
- C/C++中字符指针数组及指向指针的指针的含义
- Principle 1: Your Potential Is Unlimited
- 谈谈生成树、交换机内的生成树算法