Session Bean分为有状态Bean和无状态Bean。
Session Bean需要定义接口和Bean Class。接口分为remote和local。remote interface可以被来自EJB容器之外的应用访问到。local interface可以被同处于EJB容器内的其它应用使用。
- 定义接口方法
package com.manfred.ejb;
public interface HelloWorldRemote {
public String sayHello(String name);
}
- 定义Session Bean
package com.manfred.ejb.impl;
import java.io.Serializable;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.manfred.ejb.HelloWorldRemote;
@Stateless
@Remote({ HelloWorldRemote.class })
public class HelloWorldBean implements HelloWorldRemote, Serializable {
private static final long serialVersionUID = 1L;
public String sayHello(String name) {
return name + ", this is the first EJB";
}
}
- HelloWorld EJB发布。