zoukankan      html  css  js  c++  java
  • 可采用两种方法得到一个EJB对象

    (本文是转载其他人的技术文章,觉得说得挺浅显易懂,特借来学习) 
       在前面学习的例子中,只有一个EJB,但是对于一个真实的项目,EJB的数量可以有很多,而且EJB之间也会互相调用,那么在一个EJB里面,如何去调用另一个EJB呢?这正是本节要介绍的内容。 
       新建一个bean  
       Other.java 
       public interface Other { 
          public String sayMe(); 
       } 


       OtherBean.java 
       @Stateless 
       // 不写的话默认是本地接口 
       public class OtherBean implements Other { 
          public String sayMe() { 
            return "other"; 
          } 
       } 


       现在是要在HelloWorldBean里面调用OtherBean里面的sayMe方法。 那么我们在helloworld里面怎么获取otherbean ejb呢?有人可能去想到这种方法: 
       public class HelloWorldBean implements HelloWorld, HelloWorldLocal { 
          private Other other = new OtherBean(); 

          public String sayHello(String name) { 
              return name + "说:你好," + other.sayMe(); 
          } 
       } 


       这种方法是绝对错误的,如果通过这种方法得到的对象,它不是EJB对象,而只是一个简简     单单的Java对象。 
        我们要得到一个EJB对象,我们可以采用两种方法。一种方法是通过JNDI查找,第二种方法是采用依赖注入。那么我们首先学一下通过JNDI查找的方式来得到other EJB。 
        HelloWorldBean.java 
        @Stateful  //指明这个ejb是无状态的会话bean 
    @Remote(HelloWorld.class) //将HelloWorld这个接口指定为远程接口 
    @Local(HelloWorld.class) //将HelloWorld这个接口指定为本地接口 

    //在这个EJB里面,不单具有本地接口,也有远程接口 
    public class HelloWorldBean implements HelloWorld, HelloWorldLocal { 

        public String sayHello(String name) { 
            try { 
                InitialContext ctx = new InitialContext(); 
                Other other = (Other)ctx.lookup("OtherBean/local"); 
                return name + "说:你好," + other.sayMe(); 
            } catch (NamingException e) { 
                e.printStackTrace(); 
            } 
            return null; 
        } 



    EJBClient.java 

    public class EJBClient { 

        public static void main(String[] args) { 

            try { 
                InitialContext ctx = new InitialContext(); 
                HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote"); 

                System.out.println(helloworld.sayHello("注入者")); 
            } catch (NamingException e) { 
                System.out.println(e.getMessage()); 
            } 
        } 



    除了JNDI查找的方式外,还可以通过依赖注入的方式来得到other ejb。 

    HelloWorldBean.java 
    @Stateful  //指明这个ejb是无状态的会话bean 
    @Remote(HelloWorld.class) //将HelloWorld这个接口指定为远程接口 
    @Local(HelloWorld.class) //将HelloWorld这个接口指定为本地接口 

    //在这个EJB里面,不单具有本地接口,也有远程接口 
    public class HelloWorldBean implements HelloWorld, HelloWorldLocal { 

        @EJB Other other; //通过这个注解就可以把EJB注入进来 
        public String sayHello(String name) { 
            return name + "说:你好," + other.sayMe(); 
        } 



    大家比较一下,采用EJB注解注入other EJB,和采用JNDI查找的方式来得到EJB,在代码量上面显然@EJB注解代码量更少,而且在使用上面比JNDI的方式更优雅一些。 
        那么以后大家在做项目的时候,如果要使用到别的EJB的话,可以采用注解的方式来注入要使用的EJB。 

        @EJB Other other; 这种注入它里面的工作原理是怎样的呢? 
        大概是这样的:EJB容器当解释到类里面的字段当带有@EJB注解的时候,它就会根据这个字段other的类型也就是接口Other去寻找是否有实现了这个接口的EJB,如果寻找到了实现了Other这个接口的EJB,并且只寻找到一个的时候,那么它就会把这个EJB注入进来。 
        那么有同学会问:如果寻找到了两个EJB都实现了Other这个接口,那会出现什么问题呢? 这时候会报一个错,因为它不知道我们需要注入哪一个。 
        那么这时候又可能会有同学会问:是不是没有办法解决这个问题呢? 当然,办法是有的。 
        当出现这种情况的时候,我们就要明确指定我们需要注入的是哪一个EJB。 现在假设Other这个接口被两个EJB实现了,我们要注入的是OtherBean这个EJB,那么我们会这样: 
        @EJB(beanName="OtherBean") Other other; 通过这种形式就可以明确指定需要注入的EJB了,哪怕Other这个接口已被很多EJB实现了。 

        @EJB这个EJB注解它只能够注入EJB,其它那些资源类型是不能采用这个注解来注入的。那么好比我们现在要使用EJB里面的定时服务,这时候我们就不能使用这个@EJB来注入定时服务了。 
        那么我们应该使用哪些注解呢?应该使用@Resource注解,好比我们要注入定时服务:@Resource TimerService timerService; 注入数据源,也是用@Resource注解 
  • 相关阅读:
    ELK 一些截图
    AD域
    NPOI
    搭建harbor
    【【【【日常问题记录】】】】
    golang yaml配置文件解析
    golang操作mysql使用总结
    【转】mysql优化步骤
    【转】Mysql事务,并发问题,锁机制
    golang curl
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4379859.html
Copyright © 2011-2022 走看看