Practical jboss - teach you to write the first EJB ---- 2

xiaoxiao2021-03-06  62

Write the first EJB: "Hello, World", let's officially start EJB programming. Before writing our first EJB, you should have a roughly understanding of EJB. If there is no, it is recommended that you first go to the Internet to find some articles, otherwise you will not understand the content you want to tell.

Remote interface

Remote interface refers to the call interface that can be seen for the client.

/// * This is a remote interface. The client calls this interface to make real EJB work * / public interface helloworld extends javax.ejb.ejbobject {public string hello () throws java.rmi.RemoteException; }

HOME interface

We can see the home interface as a factory that manufactures EJB. The HOME interface tells the EJB container: "Hey, my customers want me to generate an ejb, now I will give you this task!"

//HelloWorldHome.javapackage sample; / * Home Interface EJB container tells how to generate or destroy instance of the EJB * / public interface HelloWorldHome extends javax.ejb.EJBHome {HelloWorld create () throws java.rmi.RemoteException, javax.ejb.CreateException; }

EJB implementation

Here is true EJB implementation

//HelloWorldBean.javapackage sample; import javax.ejb.SessionContext; remote interface HelloWorld * / pubic class HelloWorldBean implements javax.ejb.SessionBean {private SessionContext ctx / * class specific implementation; public void setSessionContext (SessionContext ctx) {this. CTX = CTX;} Pubic void ejbremove () {system.out.println ("ejbremove ()");} public void ejbactivate () {system.out.println ("ejbactivate ()");} public void ejbpassivate () {System.out.Println ("ejbpassivate ()");} / * Hello method is the actual business logic, which can display "Hello, World" string * / public string hello () {system.out .println ("Hello ()"); Return "Hello, World";}}

Ok, this session EJB is written, the next step we have to do is preparing its deployment file:

ejb-jar.xml

JBoss Hello World Application Hello World EJB Hello Sample.HelloHome Sample.Hello Sample.Hellobean stateless bean This EJB-jar.xml file should be put In this example, I put it in this example.

F: / projects / jbss-tutorial directory, of course, you should put it in your own project catalog, in order to make you know more clearly, let you know the location of the EJB-JAR.XML file, I will list the following. The entire directory structure of this example:

In this way, we have completed a simple session EJB writing, but in fact, JBoss also provides an additional profile: jboss.xml, using it to make more customization of JBoss servers, but because this example is too simple. So we can omit it.

Although we have completed the writing of this session EJB, there is still a final step to do: package. First we entered the root directory of the current project:

CD f: / project / jboss-tutorial

Then execute the JAR command to package all classes and ejb-jar.xml:

JAR CF HelloWorld.jar Sample Meta-INF

At this time, you will find that there is a file called helloWorld.jar in the current directory, which is our final finished product. The illustration is as follows:

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

New Post(0)