| |
EJB本地和远程接口 |
|
File: HelloServiceBean.java
import javax.ejb.Stateless;
@Stateless
public class HelloServiceBean implements HelloServiceLocal, HelloServiceRemote {
public String sayHello(String name) {
return "Hello1, " + name;
}
}
File: HelloServiceLocal.java
import javax.ejb.Local;
@Local
public interface HelloServiceLocal {
public String sayHello(String name);
}
File: HelloServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface HelloServiceRemote{
public String sayHello(String name);
}
File: 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
File: Main.java
import java.util.*;
import javax.naming.*;
public class Main{
public static void main(String[] a) throws Exception{
String name = "java2s";
HelloServiceRemote service = null;
//Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
//service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (HelloServiceRemote)new InitialContext().lookup("HelloServiceBean/remote");
System.out.println(service.sayHello(name));
}
}
|
|
EJB-EjbLocalAndRemote.zip( 4,485 k) |
Related examples in the same category |
|