开发工具:javaee版本的eclipse、JDK1.6、jboss5.0.0
注意:jboss的路径不能有中文,否则会报错。
新建ejb工程,选择jboss5和ejb3
代码:
package com.ejb;
public interface HelloWorld {
public String sayHello(String name);
}
package com.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import org.jboss.ejb3.annotation.RemoteBinding;
@Stateless
@Remote({HelloWorld.class})
@RemoteBinding(jndiBinding = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return name;
}
}
然后,就可以用jboss5运行了。
测试代码:
package com.ejb.test;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ejb.HelloWorld;
public class TestClient {
/**
* @param args
*/
public static void main(String[] args) {
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");
try {
Context ctx = new InitialContext(props);
HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorld");
System.out.println(helloworld.sayHello("hello"));
} catch (NamingException e) {
e.printStackTrace();
}
}
}