package cn.itm.ejb3;
public interface Other {
public String sayMe();
}
package cn.itm.ejb3.impl;
import javax.ejb.Stateless;
import cn.itm.ejb3.Other;
@Stateless
public class OtherBean implements Other {
public String sayMe() {
return "Other";
}
}
需求:helloworldbean 如何调用 otherbean里面的方法。
package cn.itm.ejb3;
public interface HelloWorld {
// 业务方法:
public String sayHello(String name);
}
package cn.itm.ejb3;
public interface HelloWorldLocal extends HelloWorld {
}
第一种方式:通过 JNDI方式:
package cn.itm.ejb3.impl;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import cn.itm.ejb3.HelloWorld;
import cn.itm.ejb3.HelloWorldLocal;
import cn.itm.ejb3.Other;
@Stateless // 指明EJB他是一个无状态的会话bean。
@Remote(HelloWorld.class) //
@Local(HelloWorldLocal.class) // 如果客户端 和 EJB是部署在同一个 jboss 内,我们优先调用 本地接口。
public class HelloWorldBean implements HelloWorld,HelloWorldLocal{
// @EJB Other other;
public String sayHello(String name) {
try {
InitialContext ic = new InitialContext();
Other other = (Other)ic.lookup("OtherBean/local");
return name + "说:您好," + other.sayMe();
} catch (NamingException e) {
e.printStackTrace();
}
// return name + "说:您好," + other.sayMe();
return null;
}
}
测试类,通用的。
package cn.itm.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import cn.itm.ejb3.HelloWorld;
public class EJBTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
InitialContext ic = new InitialContext( );
HelloWorld hw = (HelloWorld) ic.lookup("HelloWorldBean/remote"); // 不在同一个jvm类 只能通过远程。
String message = hw.sayHello("注入者");
System.out.println(message);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- name中指的是项目名称。basedir指的是与build.xml的同级目录 -->
<!--
(1)basedir 代表 项目所在的路径。
-->
<project name="HelloWorld" basedir=".">
<!-- 项目源文件 所在的目录。-->
<property name="src.dir" value="${basedir}\src" />
<!-- 指向环境变量中系统变量 -->
<property environment="env" />
<!-- 指向系统变量中的JBOSS_HOME变量 ,可以得知JBOSS_HOME的安装地址 -->
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<property name="jboss.server.config" value="default" />
<!-- class文件存在的目录 -->
<property name="build.dir" value="${basedir}\build" />
<path id="build.classpath">
<!-- 包含 client路径下的所有 jar文件-->
<fileset dir="${jboss.home}\client">
<include name="*.jar" />
</fileset>
<pathelement location="${build.dir}" />
</path>
<target name="prepare">
<delete dir="${build.dir}" />
<mkdir dir="${build.dir}" />
</target>
<!-- depends="prepare" 确保 先执行上面的代码,在执行下面的代码 -->
<target name="compile" depends="prepare" description="编译">
<!-- class 存放到 destdir这个目录。在调用javac 这个目录就必须建立起来-->
<javac srcdir="${src.dir}" destdir="${build.dir}" >
<classpath refid="build.classpath" />
</javac>
</target>
<target name="ejbjar" depends="compile" description="创建EJB发布包">
<jar jarfile="${basedir}\${ant.project.name}.jar">
<fileset dir="${build.dir}">
<include name="**/*.class" />
</fileset>
</jar>
</target>
<target name="deploy" depends="ejbjar" description="发布ejb">
<copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
</target>
<target name="undeploy" description="卸载ejb">
<delete
file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar" />
</target>
</project>
第二种方式:通过注解:@EJB Other other;
package cn.itm.ejb3.impl;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import cn.itm.ejb3.HelloWorld;
import cn.itm.ejb3.HelloWorldLocal;
import cn.itm.ejb3.Other;
@Stateless // 指明EJB他是一个无状态的会话bean。
@Remote(HelloWorld.class) //
@Local(HelloWorldLocal.class) // 如果客户端 和 EJB是部署在同一个 jboss 内,我们优先调用 本地接口。
public class HelloWorldBean implements HelloWorld,HelloWorldLocal{
@EJB Other other;
public String sayHello(String name) {
try {
InitialContext ic = new InitialContext();
Other other = (Other)ic.lookup("OtherBean/local");
return name + "说:您好," + other.sayMe();
} catch (NamingException e) {
e.printStackTrace();
}
return name + "说:您好," + other.sayMe();
}
}
测试类依旧用上面的。
package cn.itm.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import cn.itm.ejb3.HelloWorld;
public class EJBTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
InitialContext ic = new InitialContext( );
HelloWorld hw = (HelloWorld) ic.lookup("HelloWorldBean/remote"); // 不在同一个jvm类 只能通过远程。
String message = hw.sayHello("注入者");
System.out.println(message);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
打印结果:注入者说:您好,Other。调用成功。