龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

JBoss下的EJB3开发无状态会话Bean[组图]

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
1。开发一个具有Remote和Local接口的EJB3StatelessSessionBean. 如有那里写的不好请大家多多指教。 ############################## ##好了先介绍一下明星演员们:# ############################## !-- frame conte

  1。开发一个具有Remote和Local接口的EJB3 Stateless SessionBean.
  如有那里写的不好请大家多多指教。
  ##############################
  ## 好了先介绍一下明星演员们:#
  ##############################
   <!-- frame contents -->
<!-- /frame contents --> 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实现了业务逻辑接口进入讨论组讨论。
  
精彩图集

赞助商链接