为了存取那些服务对象,你需要通过服务器的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类中.
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
public class InjectionBean implements Injection {
@EJB (beanName="HelloWorldBean")
//@EJB (mappedName="HelloWorldBean/remote")
HelloWorld helloworld;
..
public class InjectionBean implements Injection {
HelloWorld helloworld;
@EJB (beanName="HelloWorldBean")
public void setHelloworld(HelloWorld helloworld) {
this.helloworld = helloworld;
}
..
Injection.java
package com.foshanshop.ejb3;
public interface Injection {
public String SayHello();
}
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());
}
%>
//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);
}
}
@Resource (mappedName="ConnectionFactory")
QueueConnectionFactory factory;
@Resource (mappedName="queue/A")
Queue queue;
@Resource
TimerService tms;
@Resource
SessionContext ctx;
和@EJB注释相同, @Resource注释也可以被用在JavaBean风格的setter 方法上.