服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

进阶:jboss下的ejb3开发无状态会话bean

1。开发一个具有remote和local接口的ejb3 stateless sessionbean.[:e]
如有那里写的不好请大家多多指教。
##############################
## 好了先介绍一下明星演员们:#
##############################
remote接口:remotehelloworld.java
local接口:localhelloworld.java
sessionbean:helloworldbean.java
jndi配置:jndi.properites
jsp:hello.jsp

package com.yourcompany.ejb3;
public interface remotehelloworld{
   public string say(string name);
}

package com.yourcompany.ejb3;
public interface localhelloworld{
   public string say(string name);
}

package com.yourcompany.ejb3;
import javax.ejb.stateless;
import javax.ejb.remote;
import javax.ejb.local;
import com.yourcompany.ejb3.remotehelloworld;
import com.yourcompany.ejb3.localhelloworld;

@remote({remotehelloworld.class})
//注释表示remotehelloworld为这个sessionbean的remote接口
@local({remotehelloworld.class})
//注释表示localhelloworld为这个sessionbean的local接口

//@stateless注释表示这是一个无状态会话bean
public @stateless class helloworldbean implements remotehelloworld,localhelloworld{
    public string say(string name){
       return "这是一个无状态的ejb3会话bean,作者:"+name;
    }
}
//jndi配置告诉了你的客户端初始化jndi naming service
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:1099

jsp中调用代码如下:
<%
    properties prop=new properties();
    prop.load(thread.currentthread().getcontextclassloader().getresourceasstream("jndi.properties"));
    initialcontext ctx=new initialcontext(prop);
    remotehelloworld remotehelloworld=(remotehelloworld)ctx.lookup("helloworldbean/remote");
    remotehelloworld.say("christina007[remote]");
    localhelloworld localhelloworld=(localhelloworld)ctx.lookup("helloworldbean/local");
    localhelloworld.say("christina007[local]");
%>


运行结果:
这是一个无状态的ejb3会话bean,作者:christina007[remote]

这是一个无状态的ejb3会话bean,作者:christina007[local]


总结步骤:
1.先写好了sessionbean的业务逻辑接口
2.再写sessionbean,记得在sessionbean实现了业务逻辑接口