上面,你学到了如何开发藕合松散的服务组件.但是,为了存取那些服务对象,你需要通过服务器的JNDI来查找存根对象(session bean)或消息队列(MDB).JNDI查找是把客户端与实际的服务端实现解藕的关键步骤.但是,直接使用一个字符串来进行JNDI查找并不优雅.有这 样几个原因:
?客户端与服务端必须有一致的基于字符串的名字.它没有在编译时得到认证或在布署时得到检查.
?从JNDI返回的服务对象的类型没有在编译时进行检查,有可能在运行时出现转换(casting)错误.
?冗长的查找代码,有着自己的try-catch代码块,在应用之间是重复的和杂乱的
EJB 3.0,对任何POJO,提供了一个简单的和优雅的方法来解藕服务对象和资源.使用@EJB注释,你可以将EJB存根对象注入到任何EJB 3.0容器管理的POJO中.如果注释用在一个属性变量上,容器将会在它被第一次访问之前赋值给它.在Jboss下一版本中@EJB注释从 javax.annotation包移到了javax.ejb .
下面的例子演示了怎样把HelloWorldBean无状态session bean的存根注入到InjectionBean类中.
java 代码
?
- InjectionBean.java??
- package?com.foshanshop.ejb3.impl;??
- import?com.foshanshop.ejb3.HelloWorld;??
- import?com.foshanshop.ejb3.Injection;??
- import?javax.annotation.EJB;??
- import?javax.ejb.Remote;??
- import?javax.ejb.Stateless;??
- @Stateless??
- @Remote?({Injection.class})??
- public?class?InjectionBean?implements?Injection?{??
- @EJB?(beanName="HelloWorldBean")??
- HelloWorld?helloworld;??
- public?String?SayHello()?{??
- return?helloworld.SayHello("注入者");??
- }??
- }???
@EJB注释的beanName属性指定EJB的类名(不带包名),他的另一个属性mappedName指定Bean实例的JNDI名.
下面的片断演示了如何使用beanName或mappedName属性查找HelloWorldBean 会话bean
java 代码
?
- public?class?InjectionBean?implements?Injection?{??
- @EJB?(beanName="HelloWorldBean")??
- ??
- HelloWorld?helloworld;??
- ..???
@EJB注释如果被用在JavaBean风格的setter方法上时,容器会在属性第一次使用之前,自动地用正确的参数调
用bean的setter方法.下面的片断演示了这是如何做的
java 代码
?
- public?class?InjectionBean?implements?Injection?{??
- HelloWorld?helloworld;??
- @EJB?(beanName="HelloWorldBean")??
- public?void?setHelloworld(HelloWorld?helloworld)?{??
- this.helloworld?=?helloworld;??
- }??
- ..?
?@EJB注释如果被用在JavaBean风格的setter 方法上时,容器会在属性第一次使用之前,自动地用正确的参数调 用bean的setter 方法。下面的片断演示了这是如何做的 public class InjectionBean implements Injection { HelloWorld helloworld; @EJB (beanName="HelloWorldBean") public void setHelloworld(HelloWorld helloworld) { this.helloworld = helloworld; } ….. 下面是InjectionBean的Remote业务接口 Injection.java //author:lihuoming package com.foshanshop.ejb3; public interface Injection { public String SayHello(); } 下面是Session Bean的JSP 客户端代码: Jboss EJB3.0实例教程 版权所有:黎活明 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"); 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()); } %> @EJB注释只能注入EJB存根对象,除@EJB注释之外,EJB 3.0也支持@Resource注释来注入来自JNDI的任何 资源。下面的例子中演示了如何注入数据源。"java:/DefaultMySqlDS"是数据源DefaultMySqlDS的全局JNDI名。 有关数据源的配置请参考后面章节“JBoss数据源的配置” //author:lihuoming package com.foshanshop.ejb3.impl; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.foshanshop.ejb3.HelloWorld; import com.foshanshop.ejb3.Injection; import javax.annotation.EJB; import javax.annotation.Resource; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.sql.DataSource; @Stateless @Remote ({Injection.class}) public class InjectionBean implements Injection { @EJB (beanName="HelloWorldBean") HelloWorld helloworld; @Resource (mappedName="java:/DefaultMySqlDS") Jboss EJB3.0实例教程 版权所有:黎活明 DataSource myDb; public String SayHello() { String str = ""; try { Connection conn = myDb.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT studentName FROM student"); if (rs.next()) { str = rs.getString(1); } rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } return helloworld.SayHello(str); } } 如果JNDI对象在本地(java:comp/env)JNDI目录中,你只需给定他的映谢名称即可,不需要带前缀,如下面例子注 入一个消息connection factory和一个messaging queue @Resource (mappedName="ConnectionFactory") QueueConnectionFactory factory; @Resource (mappedName="queue/A") Queue queue; 对于"well-known"对象,@Resource注释可以不指定JNDI名就能注入他们,他通过变量的类型就能获得他的JNDI 名。下面是一些例子。 @Resource TimerService tms; @Resource SessionContext ctx; 和@EJB注释相同,@Resource注释也可以被用在JavaBean风格的setter 方法上.