三 依赖注入 (dependency injection) 的学习 EJB 3.0, 提供了一个简单的和优雅的方法来解藕服务对象和资源 使用 @EJB 注释, 可以将 EJB 存根对象注入到任何 EJB 3.0 容器管理的 POJO 中 如果注释用在一个属性变量上, 容器将会在它被第一次访问之前赋值给它 在 Jboss 下一版本中 @EJB 注释从 javax.annotation 包移到了 javax.ejb 下面的例子演示了怎样把 HelloWorldBean 无状态 session bean 的存根注入到 InjectionBean 类中 InjectionBean.java import com.foshanshop.ejb3.helloworld; import com.foshanshop.ejb3.injection; import javax.annotation.ejb; import javax.ejb.remote; @Stateless @Remote ({Injection.class) @EJB (beanname="helloworldbean") HelloWorld helloworld; public String SayHello() { return helloworld.sayhello(" 注入者 "); @EJB 注释的 beanname 属性指定 EJB 的类名 ( 不带包名 ), 他的另一个属性 mappedname 指定 Bean 实例的 JNDI 名 下面的片断演示了如何使用 beanname 或 mappedname 属性查找 HelloWorldBean 会话 bean @EJB (beanname="helloworldbean") //@EJB (mappedname="helloworldbean/remote") HelloWorld helloworld;.. @EJB 注释如果被用在 JavaBean 风格的 setter 方法上时, 容器会在属性第一次使用之前, 自动地用正确的参数调用 bean 的 setter 方法 下面的片断演示了这是如何做的 HelloWorld helloworld; @EJB (beanname="helloworldbean") public void sethelloworld(helloworld helloworld) {
this.helloworld = helloworld;.. 下面是 InjectionBean 的 Remote 业务接口 Injection.java package com.foshanshop.ejb3; public interface Injection { public String SayHello(); 下面是 Session Bean 的 JSP 客户端代码 : InjectionTest.jsp <%@ page contenttype="text/html; charset=gbk"%> <%@ page import="com.foshanshop.ejb3.injection, javax.naming.*, java.util.properties"%> <% Properties props = new Properties(); props.setproperty("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); props.setproperty("java.naming.provider.url", "localhost:1099"); props.setproperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); InitialContext ctx; try { ctx = new InitialContext(props); Injection injection = (Injection) ctx.lookup("injectionbean/remote"); out.println(injection.sayhello()); catch (NamingException e) { out.println(e.getmessage()); %> 在 TONGWEB5.0 方面的改造 : HelloWorldBean 无状态 session bean 的存根注入到 InjectionBean 类中 @EJB (beanname="hellobean") @EJB 注释的 beanname 属性指定 EJB 的类名 ( 不带包名 ), 实现例子 InjectionBean.java: import com.foshanshop.ejb3.injection; import com.foshanshop.ejb3.localhello; import javax.ejb.ejb; import javax.ejb.remote;
@Stateless @Remote (Injection.class) @EJB (beanname="hellobean") LocalHello helloworld; public String SayHello() { return helloworld.sayhello(" 注入者 "); HelloManBean.java 如下 : import javax.ejb.local; import com.foshanshop.ejb3.localhello; @Stateless @Local(LocalHello.class) public class HelloManBean implements LocalHello { public String SayHello(String name) { return name+" 说,HelloManBean 你好!"; HelloBean 如下 : import javax.ejb.local; import javax.ejb.remote; import com.foshanshop.ejb3.localhello; import com.foshanshop.ejb3.remotehello; @Stateless @Remote (RemoteHello.class) @Local(LocalHello.class) public class HelloBean implements RemoteHello, LocalHello {
public String SayHello(String name) { return name+" 说,HelloBean 你好!"; 下面是 Session Bean 的 JSP 客户端代码 : InjectionTest.jsp <%@ page contenttype="text/html; charset=gbk"%> <%@ page import="com.foshanshop.ejb3.injection, javax.naming.*, java.util.properties"%> <% Properties props = new Properties(); props.setproperty("java.naming.factory.initial","com.tongweb.naming.serialinitconte xtfactory"); props.setproperty("java.naming.factory.url.pkgs","com.tongweb.naming"); props.setproperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.r mi.jndistatefactoryimpl"); props.setproperty("org.omg.corba.orbinitialhost","168.1.90.6"); props.setproperty("org.omg.corba.orbinitialport","5100"); InitialContext ctx; try { ctx = new InitialContext(props); //Injection injection = (Injection) ctx.lookup("injectionbean/remote"); Injection injection = (Injection) ctx.lookup("com.foshanshop.ejb3.injection"); out.println(injection.sayhello()); catch (NamingException e) { out.println(e.getmessage()); %> TONGWEB5.0 中依赖注入 BEAN 例子 InjectionBean 和 HelloBean 写法是一致的, 只是 Session Bean 的 JSP 客户端代码有改进 1 调用的 Properties 不同, JBOSS 调用方式 : Properties props = new Properties(); props.setproperty("java.naming.factory.initial", "org.jnp.interfaces.namingcontextfactory"); props.setproperty("java.naming.provider.url", "localhost:1099"); props.setproperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); TWEB5.0 调用方式 :
Properties props = new Properties(); props.setproperty("java.naming.factory.initial","com.tongweb.naming.serialinitcont extfactory"); props.setproperty("java.naming.factory.url.pkgs","com.tongweb.naming"); props.setproperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation. rmi.jndistatefactoryimpl"); props.setproperty("org.omg.corba.orbinitialhost","168.1.90.6"); props.setproperty("org.omg.corba.orbinitialport","5800"); 2 ctx.lookup REMOTE 方式不同 : JBOSS lookup ctx.lookup("injectionbean/remote "); TONGWEB5.0 lookup ctx.lookup(com.foshanshop.ejb3.injection) 3 修改 tongweb-ejb-jar.xml <?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE tongweb-ejb-jar PUBLIC "-//TONGTECH,CO.LTD//DTD TWNS 1.0 EJB 3.0//EN" "http://www.tongtech.com/j2ee/dtds/tongweb-ejb-jar_3_0-1.dtd"> <tongweb-ejb-jar> <enterprise-beans> <unique-id>0</unique-id> <ejb> <ejb-name>hellobean</ejb-name> <jndi-name>com.foshanshop.ejb3.remotehello</jndi-name> <local-jndi-name>com.foshanshop.ejb3.localhello</local-jndi-name> <pass-by-reference>false</pass-by-reference> <is-read-only-bean>false</is-read-only-bean> <refresh-period-in-seconds>-1</refresh-period-in-seconds> <cmt-timeout-in-seconds>0</cmt-timeout-in-seconds> <gen-classes/> </ejb> <ejb> <ejb-name>hellomanbean</ejb-name> 把 <local-jndi-name>com.foshanshop.ejb3.localhello</local-jndi-name> 修改为 : <local-jndi-name>com.foshanshop.ejb3.localhello1</local-jndi-name> 否则部署出错, 原因 JNDI 的名字重复了 <pass-by-reference>false</pass-by-reference> <is-read-only-bean>false</is-read-only-bean> <refresh-period-in-seconds>-1</refresh-period-in-seconds> <cmt-timeout-in-seconds>0</cmt-timeout-in-seconds> <gen-classes/>
</ejb> <ejb> <ejb-name>injectionbean</ejb-name> <jndi-name>com.foshanshop.ejb3.injection</jndi-name> <pass-by-reference>false</pass-by-reference> <is-read-only-bean>false</is-read-only-bean> <refresh-period-in-seconds>-1</refresh-period-in-seconds> <cmt-timeout-in-seconds>0</cmt-timeout-in-seconds> <gen-classes/> </ejb> </enterprise-beans> </tongweb-ejb-jar> 附件是调试好的应用程序 : 结果 : 注入者说,HelloBean 你好!