假设我已经把EJB做好了,是一个HelloWorld,下面说如何发布 Bean文件如下:
package han.ejb;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
/**
* XDoclet-based session bean. The class must be declared
* public according to the EJB specification.
*
* To generate the EJB related files to this EJB:
*
- Add Standard EJB module to XDoclet project properties
*
- Customize XDoclet configuration for your appserver
*
- Run XDoclet
* Below are the xdoclet-related tags needed for this EJB.
* @ejb.bean name="HelloWorldBean"
*
display-name="HelloWorld"
*
description="Description for HelloWorld"
*
jndi-name="ejb/HelloWorld"
*
remote="HelloWorldRemote"
*
type="Stateless"
*
view-type="remote"
*/
public class HelloWorldBean implements SessionBean {
private static final long serialVersionUID = 1L;
public HelloWorldBean() {
// TODO Auto-generated constructor stub
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
/**
* Set the associated session context. The container calls this method
* after the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
}
/**
* An ejbCreate method as required by the EJB specification.
*
* The container calls the instance?s
ejbCreate
method whose
* signature matches the signature of the
create
method invoked
* by the client. The input parameters sent from the client are passed to
* the
ejbCreate
method. Each session bean class must have at
* least one
ejbCreate
method. The number and signatures
* of a session bean?s
create
methods are specific to each
* session bean class.
*
* @throws CreateException Thrown if method fails due to system-level error.
*
* @ejb.create-method
*
*/
public void ejbCreate() throws CreateException {
// TODO Add ejbCreate method implementation
}
/**
* An example business method
*
* @ejb.interface-method view-type = "remote"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
@RemoteMethod()
@WebMethod()
public String sayhello() throws EJBException {
// rename and start putting your business logic here
return new String("Hello EJB!");
}
}
事实上JSR 181是BEA提出的用于加速Web Services开发的一种基于注释驱动的编程模式,并被批准纳入到J2EE 1.5标准。JSR181提供了一种简单的Web Service开发编程模型和标准的编译及部署方式。只需要编写JSR-175风格的注释就可以制定WSDL,消息产生属性,安全认证方式,以及特定的消 息头。
1.
所以,如果要把
SESSION BEAN
发布为
WEB
服务,必须在
JDK 1.5
下做,安装
MyEclipse
的话,
JDK
会自动被升级到
1.6,
所以要先改回来。
Project->properties->java build path,
选中
JDK
,
EDIT
从
Alternate JRE
中选
JDK 1.5
,注意,如果是自己的
JDK
,版本一定不能高过
BEA
带的
JDK
2.
添加注释
@JndiName
(remote=
"han.interfaces.HelloWorldBean"
)
@WebService
(name=
"HelloWorldPortType"
, //
端口名
serviceName=
"HelloWorldService"
, //
服务名
targetNamespace=
"http://ejb.han"
) //WebService
注释是必须有的,下面的
WLHttpTransport
和
SOAPBinding
是推荐的
@WLHttpTransport
(contextPath=
"Hello_EJB"
,
serviceUri=
"HelloWorldService"
, //http://localhost:7001/contextPath/services/serviceUri?wsdl
portName=
"HelloWorldPort"
)
@SOAPBinding
(style=
SOAPBinding
.Style.
DOCUMENT
, //
好象都这么写
use=
SOAPBinding
.Use.
LITERAL
,
parameterStyle=
SOAPBinding
.ParameterStyle.
WRAPPED
)
添加业务逻辑注释
@RemoteMethod
()
@WebMethod
()
导入相关包
import
weblogic.jws.WLHttpTransport;
import
javax.jws.WebMethod;
import
javax.jws.WebService;
import
javax.jws.soap.SOAPBinding;
import
weblogic.ejbgen.JndiName;
import
weblogic.ejbgen.RemoteMethod;
import
weblogic.jws.WLHttpTransport;
3.
创建
build.xml
文件(注释是后写的,所以不符合规范)
4.Run Ant
会发现生成了两个文件(application.xml和weblogic-application.xml)和一个JAR(jws.jar)包
5.
部署
启动服务器,进入console,手动部署
处于ACTIVE状态
然后写个客户断就可以测试了