SessionBean
1.新建Ejb Project
2.编写接口
package com.tgb.ejb;
publicinterface FirstEjb{
StringsayHello(String name);
}
3.编写实现类
package com.tgb.ejb;
publicclass FirstEjbBean implements FirstEjb{
publicString sayHello(String name){
return“Hello,” + name;
}
}
4.利用注解配置EJB
//此处可以加入name属性,规定这个bean的名称
@Stateless
@Remote
publicclass FirstEjbBean implements FirstEjb{
publicString sayHello(String name){
return“Hello,” + name;
}
}
5.部署到jboss并运行jboss
6.将EJB项目的接口打包(客户端只需要EJB接口即可),并把这个jar包加入到客户端项目的类路径下
7.将jboss安装目录下的client目录里面所有jar加入到客户端项目中
8.编写客户端代码
packagecom.tgb.ejb;
importjavax.naming.InitialContext;
importjavax.naming.NamingException;
publicclass FirstEjbClient{
publicstatic void main(String[] args) throwsNamingException{
InitialContextcontext = new InitialContext();
FirstEjbejb = (FirstEjb)context.lookup(“FirstEjbBean/remote”);
Strings = ejb.sayHello(“zhangsan”);
System.err.println(s);
}
}
9.在客户端源代码目录下添加jndi.properties
java.naming.factory.initial =org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs= org.jboss.naming:org.jnp.interfaces
java.naming.provider.url= localhost
如果本地客户端访问EJB,要修改JBOSS_HOME/SERVER/DEFAULT/DEPLOY/JBOSS-WEB.DEPLOYER/META-INF/JBOSS-SERVICE.XML文件中 ,由FALSE改为TRUE