参照itcast的视频教程开始了我的EJB学习,写了第一个Stateless的Remote的Bean
/** * */package com.cqzhu.ejb3;/** * @author PowerLinux * */public interface HelloWorld { public String sayHello(String name);}
/** * */package com.cqzhu.ejb3.impl;import javax.ejb.Remote;import javax.ejb.Stateless;import com.cqzhu.ejb3.HelloWorld;/** * @author PowerLinux * */@Stateless@Remote(HelloWorld.class)public class HelloWorldBean implements HelloWorld { /* (non-Javadoc) * @see com.cqzhu.ejb3.HelloWorld#sayHello(java.lang.String) */ @Override public String sayHello(String name) { // TODO Auto-generated method stub return name+"Say Hello,World!"; }}client:
package com.cqzhu.ejb3.test;import java.util.Properties;import javax.naming.InitialContext;import javax.naming.NamingException;import com.cqzhu.ejb3.HelloWorld;public class EJBClient { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Properties props = new Properties(); props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); props.setProperty("java.naming.provider.url", "localhost:1099"); try { InitialContext ctx = new InitialContext(); HelloWorld helloworld = (HelloWorld)ctx.lookup("HelloWorldBean/Remote"); System.out.println(helloworld.sayHello("PowerLinux ")); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}运行:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) at javax.naming.InitialContext.lookup(InitialContext.java:392) at com.cqzhu.ejb3.test.EJBClient.main(EJBClient.java:24)出错原因:
InitialContext ctx = new InitialContext();初始化出错,应为:
InitialContext ctx = new InitialContext(props);
再次运行:
javax.naming.NameNotFoundException: Remote not bound
出粗原因:
HelloWorld helloworld = (HelloWorld)ctx.lookup("HelloWorldBean/Remote");应为:
HelloWorld helloworld = (HelloWorld)ctx.lookup("HelloWorldBean/remote");
运行:
PowerLinuxSay Hello,World!