关于EJB3的一个简单例子

EJB3已经变得很简单了,在这记上一笔。

    关于EJB3,可参阅:http://www.foshanshop.net/

    服务器选择Jboss。客户端需要导入Jboss/client/jbossall-client.jar才能调用发布在Jboss中的EJB

    代码:

    HelloBeanLocal.java

    view plaincopy to clipboardprint?
    package session;

    import javax.ejb.Local;

    @Local
    public interface HelloBeanLocal {
    }

    package session;

    import javax.ejb.Local;

    @Local
    public interface HelloBeanLocal {
    }

    HelloBeanRemote.java

    view plaincopy to clipboardprint?
    package session;

    import javax.ejb.Remote;

    @Remote
    public interface HelloBeanRemote {

        public String sayHello(String name);
    }

    package session;

    import javax.ejb.Remote;

    @Remote
    public interface HelloBeanRemote {

     public String sayHello(String name);
    }

    HelloBean.java

    view plaincopy to clipboardprint?
    package session;

    import javax.ejb.Stateless;

    @Stateless
    public class HelloBean implements HelloBeanLocal, HelloBeanRemote {

        public String sayHello(String name) {

            System.out.println("服务器端输出:" + name);
            return "Hello, " + name;
        }
    }

    package session;

    import javax.ejb.Stateless;

    @Stateless
    public class HelloBean implements HelloBeanLocal, HelloBeanRemote {

     public String sayHello(String name) {

      System.out.println("服务器端输出:" + name);
      return "Hello, " + name;
     }
    }

    HelloBeanClient.java

    view plaincopy to clipboardprint?
    import java.util.*;
    import javax.naming.*;
    public class HelloBeanClient {

        public static void main(String[] args)throws Exception {

    //      Hashtable properties=new Hashtable();
    //      properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
    //      properties.put(Context.PROVIDER_URL,"jnp://127.0.0.1");
    //      InitialContext ctx=new javax.naming.InitialContext(properties);

            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
            InitialContext ctx = new InitialContext(props);

            session.HelloBeanRemote remote=(session.HelloBeanRemote)ctx.lookup("HelloBean/remote");
            System.out.println(remote.sayHello("Beijing"));
            ctx.close();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值