posts - 195, comments - 34, trackbacks - 0, articles - 1

jndi全局注册表和enc的基本概念 zz

Posted on 2009-03-24 10:26 小强摩羯座 阅读(408) 评论(0)  编辑  收藏 所属分类: Java
jndi全局注册表和enc的基本概念
   通俗的将jndi就是对ejb容器中所有的资源和组件进行管理的花名册,通过该服务,可以很方便的找到希望使用的资源。当组件被部署到服务器上后,该组件就会被自动注册到花名册上,以便用户使用,如果组件被卸载,花名册上的记录就会自动消失。
    jndi中注册的所有的资源和组件可以被服务器内外的各种程序请求或者访问,是一种全局性的资源!但是ejb中不同的组件通过全局性的jndi服务器形成依赖关系,则会给系统造成不稳定的因素!因此就引入了enc(ejb组件的企业名称上下文)的概念!通过她来实现不同组件之间的引用关系!!!!
在上一篇文章中写到了通过标注的方式实现方向依赖注入!还以上篇为例:
有接口:HelloWordRemote  HelloWordLocal ICal(Remote)
而HelloWordBean实现了分别实现了他们
另外又有了个远程接口:
@Remote
public Interface MyService{
public String helloWord();
public int add(int a,int b);
}
一个类想实现这个接口并且在这个类中引用了...如下:
@Stateless
public class FacedServcie implements MyService{
private HelloWordLocal myserv1;
private ICal ical;
.....
....
}
可以通过配置文件实现反向依赖注入:
<ejb-jar
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                           http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
       version="3.0">
   <enterprise-beans>
      <session>
         <ejb-name>FacedServcie</ejb-name>
         <ejb-ref>
           <ejb-ref-name>abc1</ejb-ref-name>
           <ejb-ref-type>Session</ejb-ref-type>
           <remote>com.ICal</remote>
           <mapped-name>HelloWordBean1/remote</mapped-name>
           <injection-target>
              <injection-target-class>
                 xiaoxiao.FacedService
              </injection-target-class>
              <injection-target-name>
                 ical
              </injection-target-name>
           </injection-target>
         </ejb-ref>
.........(对于另一个组件的配置)
      </session>
   </enterprise-beans>
</ejb-jar>
还可以通过检索的方式来实现:
@Stateless

public class FacedServcie implements MyService{
private HelloWordLocal myserv1;
private ICal ical;
public String helloWord(){
try{
InitialContext ctx=new InitalContext();
ical=(ICal)ctx.lookup("java:comp/env/abc1");
//其中java:comp/env是组件局部enc所有记录的根路径而abc1是在配置文件中注册的名字!
}catch(NamingException e){}
if(ical!=null){
return ical.helloWord();
}else{
return "error!";
}
}
....
}
配置文件如下:
<ejb-jar
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                           http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
       version="3.0">
   <enterprise-beans>
      <session>
         <ejb-name>FacedServcie</ejb-name>
         <ejb-ref>
           <ejb-ref-name>abc1</ejb-ref-name>
           <ejb-ref-type>Session</ejb-ref-type>
           <remote>com.ICal</remote>
           <mapped-name>HelloWordBean1/remote</mapped-name>         
         </ejb-ref>
.........(对于另一个组件的配置)
      </session>
   </enterprise-beans>
</ejb-jar>
本人建议使用第一种反向依赖注入的方式!
还有其他的一些注入:如持久化单元注入,资源型注入 数据源类型的注入。。。



只有注册用户登录后才能发表评论。


网站导航: