JBoss 5实现
1 建一个EJBProject
2写一个接口
package ejb3inaction.example;
import javax.ejb.Remote;
@Remote
public interface HelloUser {
public void sayHello(String name) ;
}
3写一个实现
package ejb3inaction.example;
import javax.ejb.Stateless;
@Stateless(mappedName="helloUserBean")
public class HelloUserBean implements HelloUser {
public void sayHello(String name) {
System.out.println("Hello " + name + " welcome to ejb3!") ;
}
}
4部署到JBoss
5测试
可以新建一个web项目,在src加入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:1099
写一个测试的类
package ejb3inaction.example.test;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb3inaction.example.HelloUser;
public class HelloUserTest {
public static void main(String[] args)
{
Context context = null ;
try {
context = new InitialContext() ;
HelloUser helloUser = (HelloUser)context.lookup("helloUserBean") ;
helloUser.sayHello("gswang") ;
} catch (NamingException e) {
e.printStackTrace();
}
}
}