Develop EJB using XDoclet

zhaozj2021-02-16  131

Have you developed EJB? Are you frustrating when you go to create and operate the EJB XML deployment description and interface? I have ever had.

I have recently developed an open source project called Xbeans with EJB, and decided to use another open source tool - XDoclet - to generate an XML description and EJB interface.

Use XDoclet to make you more efficiently within the J2EE framework. You will have a simpler view and relationship between Beans,

And you can remove a lot of your troubles in development.

This article will discuss xDoclets and how to use and expand it. Here, we will create a session bean using the Javadoc tag (TAG) and run it using the xdoclet.

What is xdoclet?

XDoclet is a tool in which the EJBDoclet foundation created in Rickard Oberg. Its ideas are simple: through the Bean class itself to view the entire component, not by operating multiple EJB files. How did it do? In addition to the JavaDoc mark, Java is not "attribute" promoted by .NET. We can put a special @ label in the Javadoc comment, let a doclet tool to find these labels, and then generate the appropriate XML description and interface after the information given by the bean. The xdoclet is based on EJBDoclet, and this framework extends outside the EJB field. Now you can generate Web Services, Web Applicaiton's description, even through extensions to meet your personal needs.

@ Tags There is a standard format: a "namespace" and a tag name (Tagname) that belongs to the namespace, then passed to the label via the attribute of Name = "VALUE".

Below is an universal example:

/ **

* @Namespace: tag name = "value" name2 = "value2" ...

* /

The current namespace is:

EJB: Standard EJB information (excluding specific manufacturers)

JBoss: Specific information about JBoss application servers

WebLogic: Specific information about the Bea's WebLogic application server

WEGSPHERE: Specific information about IBM's WebSphere Application Server

Orion: About Orion Application Server (Oracle) Specific Information

CASTOR: Generate related mappings according to the Castor framework

MVCSoft: Generates related files based on MVCSoft EJB2.0's persistent manager

SOAP: Generate SOAP Description

Structs: Generate structs-config.xml according to Form and Action

Web: Generate web.xml configuration according to Web Application

JSP: Generate TLD Information

As you can see, in addition to EJB support a lot of other frameworks, all EJBDoclets are renamed into xdoclets. (Translator: Of course, the framework now supported, including recent popular Spring, Hibernate, etc., specific reference official website

http://xdoclet.sourceforge.net)

Session Bean: Create a session bean by using a special JavaDoc tag

Since we have talked about XDoclet, let's come to a real example. We will start with a session EJB. This EJB is part of the Xbeans framework, but here we don't care about it. We just care about how to mark this bean class through the Javadoc tag, then use XDoclet to generate our meta files.

ReceiverBean.java will contain methods: DocumentReady (Document Doc). In this Xbean Link, this method uses a DOM document (Document) parameter and passes it to the next Xbean. Definition of class level tag

In the class level, we need to define:

1. This is a stateless session bean.

2. JNDI name

3. Environmental entity

4. WEBLOGIC POOLING Information)

Tags: @ejb: bean

In this tag, the only xdoclet property to be set is the name of the bean. We will also define the type of bean, bind to the JNDI name and display name of the customer pile:

/ ** * this is the ejb receiver xbean * * @ejb: bean type = "stateless" * name = "ejbreceiver" * jndi-name = "org.xbeans.ejb.receiver.receiver" * display-name = "EJB Receiver Xbean "* ... Other Javadoc Tags ... * / public class receiverbean imports sessionbean, domsource {

EJB: Bean's most commonly used attribute is: Name: EJB name (used in description) TYPE: Defines the "type" of bean, the session bean, is Stateful or Stateless, the entity bean is CMP or BMP JNDI-NAME: JNDI Names will be used in a specific manufacturer's deployment description (for remote interface) local-jndi-name: and JNDI-NAME, except for the local interface. View-type: Description Bean will support the interface view (views), which can be Remote, Local or Both. Being a full tab, you can view the XDoclet documentation to learn all options.

Tags: @ejb: env-entry

This tab defines an environment entity that is configured in JNDI via a special Java: Comp / ENV context. We will define an environment entity, in which the bean will look for the next Xbean in the list.

/ ** * this is the ejb receiver xbean * * ... Other javadoc tags ... * * @ejb: env-entry name = "channelbean" type = "java.lang.string" * value = "com.Your .Channelbean "* ... Other javadoc tags ... * / public class receiverbean implements sessionbean, domsource {

Tags: @Weblogic: pool

Now we will use WebLogic parameters to configure a particular manufacturer's pool feature. In order to indicate that we are using a specific manufacturer, I use the WebLogic namespace:

/ ** * this is the ejb receiver xbean * * ... Other javadoc tags ... * * @WebLogic: pool max-beans-in-free-pool = "1000" * initial-beans-in-free-pool = "10" * * ... Other javadoc tags ... * / public class receiverbean imports sessionbean, Domsource {This tag will configure the parameters of the pool in WebLogic Deployment Description File (WebLogic-EJB-JAR.XML). Of course, in the deployment, you can also use the labels of many other class levels. The following is a glimpse of the standard label that is often used:

@ejb: bean This is the only tag that requires a must-have, it is used to configure the basic information of beans @EJB: Home This tag provides information on the HOME interface. You can let XDoclets inherit a custom interface that can generate the home interface (None, Remote, Local, or Both), what should be, etc. @ejb: interface and home tags, except for configurable Information (remote and / or local) of the interface. @ejb: Finder defines the method of the lookup in the Home interface of the entity beans, defines the selected method @EJB: PK for the entity bean in the home interface of entity Beans. XDoclets can automatically generate primary key classes for you. @ejb: Data-Object You can automatically generate data objects of entity Beans through this label. @EJB: EJB-REF Configuring EJB References @EJB: EJB-External-Refiguration To reference Beans in other applications. Here, you need to enter the type of bean, the class of the Home / Remote interface, etc. @ejb: resource-ref Configure resource references. @EJB: Security-Role-REF Configuring Security Roles Reference @ejb: Transaction defines transaction types for all methods of transactional Remote and HOME interfaces. The transaction tag of a single method can be overwritten. @ejb: Permission allows all methods of REMOTE and HOME interfaces to be called based on roles. @EJB: Security-Identity is used to specify whether the caller's security ID is required when executing the EJB method, or whether a special run identifier is required.

Method level label definition

If we want a method to become part of the Remote interface, you only need to pass a method level of tags. Examples are as follows:

/ ** * The method That The sender Uses to pass the document * * @EJB: interface-method view-type = "remote" * / public void documentReady (this label is a must. You will complete these methods in the Bean class, and if you want the client to access it, add this label at your method statement. If you want to access only by local interface, you only need to simply modify the view-type value to "local". Here are some other EJB method level tags:

@ejb: rate Defines the relationship between EJB2.0 CMP entity bean @ejb: Home-method defines the method to EJBHOME * Method @ejb: Persistent-Field Create CMP in the generated CMP layer type (Layer of Type) Field "X" and specifically implements the getX / setx method. For BMP, it will generate a Getx / SetX method that keeps dirty flags (for easy ejbstore to call when needed) @ejb: pk-field logo primary key field @EJB: Transaction gives the method Definition transaction behavior ( Can only be the following valid properties: NotSupported | Supports | Required | Requiresnew | Mandatory | Never. @ EJB: Permissions for Permission Definition Method (Separated role list will be allowed to access this method). Use XDoclet to build bean

Now we already have a source file for ReceiverBean.java using xdoclet. Now you need to run xdoclets to generate all we need. A better way is to build a system using Jakarta-Ant. Ant is a universal use system based on Java. If you haven't used Ant yet, then download one to the official website of Jakarta, or refer to Open Source Java: Ant

XDoclet's group has developed Ant task (task), so we only need to simply join the build file (build.xml) can be used. There are two main tasks:

with

Since we are using EJB, let's take a closer study in the build file (Target Please refer to the ANT document in detail).

ClassName = "xdoclet.ejb.ejbdoclettask" classpath = "$ {java.class.path}; $ {xdoclet.jar.path};

$ {log4j.jar.path}; $ {ant.jar.path} "/>

SourcePath = "$ {java.dir}"

destdir = "$ {generated.java.dir}"

EJBSPEC = "2.0">

Version = "2.4" xmlencoding = "UTF-8" />

XmlenCoding = "UTF-8"

ValidateXML = "true" />

There are too many information in the sample file, so we need to stop discussing some matter:

label:

We need to let Ant understand

label. We need to define the name of the label to implement the label class, but also pass this class to the classpath.

In the example, we use the Properties that build.xml starts initialization Properties to specify the libraries we need.

ClassPath = "$ {java.class.path}; $ {xdoclet.jar.path}; $ {log4j.jar.path};

$ {Ant.jar.path} "/>

label:

The outermost label allows Ant to run the EJBDoclet task. We pass the location of our code, will generate XML descriptions and Java source code, and our version of the EJB specification we want to reference (we use Properties again):

SourcePath = "$ {java.dir}"

destdir = "$ {generated.java.dir}"

EJBSPEC = "2.0"> Tags:

We use

Directive to tell

Where you can find our beans.

In the example, we will find all the files for receiverbean.java in subdirects of $ {java.dir}.

Standard Tags: Interface and XML Deployment Description

The next series of tags will generate remote interfaces, HOME interfaces, and standard XML deployment descriptions (EJB-JAR.XML):

Specific supplier's label: Interface and XML deployment description

The last label generates a deployment description of a particular vendor. If you only deploy an EJB server, you only need one of the task. Since I intend to make Xbean EJB on as many application servers as possible, I have added all the main providers' labels.

Version = "2.4" xmlencoding = "UTF-8" />

XmlenCoding = "UTF-8"

ValidateXML = "true" />

Now I just need to run Ant in the same directory of my build file, you can build everything in Xbean EJB. Just I will determine the Ant library (Ant.jar, Jaxp.jar, an XML parser) and xdoclet.ja must be in my ClassPath environment variable, such as in the Windows environment:% set classpath =% classpath%; C: /LIB/ANT.JAR ;/lib/jaxp.jar; c: /lib/xdoclet.jar; c: /lib/log4j.jar; c: / lib / ejb. jar

% Ant Ejbdoclet [Runs Ant on the EJBDoclet Task]

% Ant [Runs Ant On The Default Task. in this case, xml descriptors, the compile the, and package everying as an ejb-jar file]

Download source code

About author

Dion Almaer is the Senior Technical Expert of The MiddleWare Company, the main designer of the J2EE community of THSERVERSIDE.COM

转载请注明原文地址:https://www.9cbs.com/read-8034.html

New Post(0)