For EJB development, you must first find it in the Client Directory of the JBoss installation path. Generally, all jar files under the Client Directory are added to the class path of the project.
Write an interface, and then write a class that implements the interface. The following example shows the call between beans,CodeHave detailed annotations
Example:
1. Write Interfaces
Other
PackageCom. tjp. ejb3;
Public InterfaceOther {
PublicString say (string Str );
}
Helloword
PackageCom. tjp. ejb3;
Public InterfaceHelloword {
PublicString sayhello (string Str );
}
Hellowordlocal
PackageCom. tjp. ejb3;
Public InterfaceHellowordlocalExtendsHelloword {
}
2. Compile the implementation class
Otherbean
Package Com. tjp. ejb3.impl;
Import Javax. EJB. stateless;
Import Com. tjp. ejb3.other;
/**
*
* Stateless Bean
* The Local interface is used by default.
* @ Author Tanjianping
*
*/
@ Stateless
Public Class Otherbean Implements Other {
@ Override
Public String say (string Str ){
// Todo auto-generated method stub
Return STR;
}
}
Hellowordbean
Package Com. tjp. ejb3.impl;
Import Javax. Activation. datasource;
Import Javax. annotation. resource;
Import Javax. EJB. EJB;
Import Javax. EJB. Local;
Import Javax. EJB. Remote;
Import Javax. EJB. stateful;
Import Javax. EJB. stateless;
Import Javax. Naming. initialcontext;
Import Org. Omg. costime. timeservice;
Import Com. tjp. ejb3.helloword;
Import Com. tjp. ejb3.hellowordlocal;
Import Com. tjp. ejb3.other;
/**
*
* @ Stateless bean, @ stateful is stateful Bean
* @ Remote (helloword. Class) helloword is a remote interface. If this parameter is left blank, it is a local interface.
* @ Local (hellowordlocal. Class) Local interface
* @ Author Tanjianping
*
*/
@ Stateful
@ Remote (helloword. Class )
@ Local (hellowordlocal. Class )
Public Class Hellowordbean Implements Helloword, hellowordlocal {
@ EJB (beanname = " Otherbean/local " )
Other; // Injection of other ejbs using annotations
@ Resource timeservice; // Scheduled service resource introduces other services
@ Resource (mappedname = " Java: XXX " ) Datasource; // Reference Data Source
@ Override
Public String sayhello (string Str ){
/* Try {
Initialcontext CTX = new initialcontext (); // use the attribute File
Other = (other) CTX. Lookup ("otherbean/local ");
Return "Other said:" + other. Say (STR );
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} */
Return " Other said: " + Other. Say (STR );
}
}
Build. xml
<? XML version = "1.0" encoding = "UTF-8" ?>
<! -- Name indicates the directory where the project name basedir project is located, 1 point indicates that the project is in the same directory of build. XML, and 2 points indicates the parent directory of build. xml. -->
< Project Name = "Helloejb" Basedir = "." >
<! -- The $ {basedir} directory of the specified project source file indicates that the source directory is under the SRC of the project. -->
< Property Name = "Src. dir" Value = "$ {Basedir}/src" />
<! -- Environment variable to the operating system: Environment = "env" -->
< Property Environment = "Env" />
<! -- $ {Env. jboss_home} The jboos environment variable is the installation directory. -->
< Property Name = "JBoss. Home" Value = "$ {Env. jboss_home }" />
<! -- Configuration items currently used by JBoss -->
< Property Name = "JBoss. server. config" Value = "Default" />
<! -- $ {Basedir}/build the directory where the source file class is stored -->
< Property Name = "Build. dir" Value = "$ {Basedir}/build" />
< Path ID = "Build. classpath" >
<! -- Convert all the jar packages of the client under JBoss -->
< Fileset Dir = "$ {JBoss. Home}/client" >
< Include Name = "*. Jar" />
</ Fileset >
< Pathelement Location = "$ {Build. dir }" />
</ Path >
<! -- Define a job to create a directory for storing class files. -->
< Target Name = "Prepare" >
< Delete Dir = "$ {Build. dir }" />
< Mkdir Dir = "$ {Build. dir }" />
</ Target >
<! -- Depends = "prepare": run the prepare task first. -->
< Target Name = "Compile" Depends = "Prepare" Description = "Compile" >
< Javac Srcdir = "$ {SRC. dir }" Destdir = "$ {Build. dir }" >
< Classpath RefID = "Build. classpath" />
</ Javac >
</ Target >
< Target Name = "Ejbjar" Depends = "Compile" Description = "Create an EJB release package" >
< Jar Jarfile = "$ {Basedir}/$ {ant. Project. name}. Jar" >
< Fileset Dir = "$ {Build. dir }" >
< Include Name = "**/*. Class" />
</ Fileset >
</ Jar >
</ Target >
< Target Name = "Deploy" Depends = "Ejbjar" Description = "Publishing EJB" >
< Copy File = "$ {Basedir}/$ {ant. Project. name}. Jar"
Todir = "$ {JBoss. Home}/Server/$ {JBoss. server. config}/deploy" />
</ Target >
< Target Name = "Undeploy" Description = "Uninstall EJB" >
< Delete
File = "$ {JBoss. Home}/Server/$ {JBoss. server. config}/deploy" />
</ Target >
</ Project >