现在的位置: 首页 > 综合 > 正文

EJB3.0中,在一个Bean中使用另外一个Bean

2013年08月09日 ⁄ 综合 ⁄ 共 3731字 ⁄ 字号 评论关闭

接口HelloWorld.java

  1. package jCuckoo;
  2. public interface HelloWorld {
  3.     public String sayHello(String name);
  4. }

接口Other.java

  1. package jCuckoo;
  2. public interface Other {
  3.     public String sayMe();
  4. }

本地接口HelloWorldLocal.java

  1. package jCuckoo;
  2. public interface HelloWorldLocal extends HelloWorld{
  3. }

实现类OtherBean.java

  1. package jCuckoo.impl;
  2. import javax.ejb.Local;
  3. import javax.ejb.Stateless;
  4. import jCuckoo.Other;
  5. @Stateless
  6. @Local(Other.class)//默认注解是Local
  7. public class OtherBean implements Other {
  8.     public String sayMe() {
  9.         return "other";
  10.     }
  11. }

实现类HelloWorldBean.java
方法一:通过JNDI进行查找实现调用另外一个Bean

  1. @Stateless
  2. //如果没有下面remote和local注解,默认是local
  3. @Remote(HelloWorld.class)
  4. @Local(HelloWorldLocal.class)
  5. public class HelloWorldBean implements HelloWorld,HelloWorldLocal {
  6.     public String sayHello(String name) {
  7.         
  8.         //通过JNDI进行查找
  9.         try {
  10.             InitialContext ctx = new InitialContext();
  11.             Other other=(Other)ctx.lookup("OtherBean/local");
  12.             return name+"说:你好,"+other.sayMe();
  13.         } catch (NamingException e) {
  14.             e.printStackTrace();
  15.         }
  16.         return null;
  17.         
  18.     }
  19. }

方法二:通过EJB注解实现调用另外一个Bean

  1. @Stateless
  2. //如果没有下面remote和local注解,默认是local
  3. @Remote(HelloWorld.class)
  4. @Local(HelloWorldLocal.class)
  5. public class HelloWorldBean implements HelloWorld,HelloWorldLocal {
  6.     //@EJB Other other;//通过注入方式实现查找
  7.     //如果注入时发现两个相同类型的other的时候,会报错.这时候需要指定对应的beanName,这样哪怕同时有多个类实现other接口,也不会出现问题
  8.     @EJB (beanName="OtherBean")Other other;
  9.     public String sayHello(String name) {
  10.         return name+"说:你好,"+other.sayMe();
  11.     }
  12. }

其它注解:
 
 //TimerService是EJB3中已知服务,可以采用Resource来注解
 @Resource TimerService timerService;
 
 //指定数据源也通过Resource来注解,并通过参数mappedName指定对应JNDI名称
 @Resource (mappedName="") DataSource dataSource;

发布,可以通过多种方式,这里采用ant发布:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project basedir="."  name="EJBTest">
  3.     <property name="src.dir" value="${basedir}/src"/>
  4.     <property environment="env"/>
  5.     <property name="jboss.home" value="${env.JBOSS_HOME}"/>
  6.     <property name="jboss.server.config" value="default"/>
  7.     <property name="build.dir" value="${basedir}/build"/>
  8.     
  9.     <path id="build.classpath">
  10.        <fileset dir="${jboss.home}/client">
  11.         <include name="*.jar"/>
  12.        </fileset>
  13.        <pathelement location="${build.dir}"/>
  14.     </path>
  15.     <target name="prepare">
  16.         <delete dir="${build.dir}"/>
  17.         <mkdir dir="${build.dir}"/>
  18.     </target>
  19.     <target name="complie" depends="prepare" description="编译">
  20.         <javac srcdir="${src.dir}" destdir="${build.dir}">
  21.             <classpath refid="build.classpath"/>
  22.         </javac>
  23.     </target>
  24.     <target name="ejbjar" depends="complie" description="创建EJB发布包">
  25.         <jar destfile="${basedir}/${ant.project.name}.jar">
  26.             <fileset dir="${build.dir}">
  27.                 <include name="**/*.class"/>
  28.             </fileset>
  29.         </jar>
  30.     </target>
  31.     <target name="deploy" depends="ejbjar" description="发布ejb">
  32.         <copy file="${basedir}/${ant.project.name}.jar" todir="${jboss.home}/server/${jboss.server.config}/deploy"></copy>
  33.     </target>
  34.     <target name="undeplay" description="卸载ejb">
  35.         <delete file="${jboss.home}/server/${jboss.server.config}/deploy/${ant.project.name}.jar"></delete>
  36.     </target>
  37. </project>

启动JBoss服务器。

接下来构建客户端EJBClient.java

  1. package jCuckoo.client;
  2. import jCuckoo.HelloWorld;
  3. import javax.naming.InitialContext;
  4. import javax.naming.NamingException;
  5. public class EJBClient {
  6.     public static void main(String[] args) {
  7.         try {
  8.             InitialContext ctx=new InitialContext();
  9.             HelloWorld helloworld=(HelloWorld)ctx.lookup("HelloWorldBean/remote");
  10.             System.out.println(helloworld.sayHello("远程小麦"));
  11.         } catch (NamingException e) {
  12.             System.out.println(e.getMessage());
  13.         }
  14.     }
  15. }

运行,可以看到对应的结果:

  1. 远程小麦说:你好,other

抱歉!评论已关闭.