1. Environment configuration
· Eclipse Luna Service Release 1 (4.4.1)
· JBOSS as 5.0
· Java 1.8.0_25
2. Create an EJB project
Click File → New → EJB project, and fill in the project name as follows:
Then click Next → Finish;
Select the Ejbmodule folder under the EJB project and right-click → New→ Session bean (EJB 3.X) to create a session bean.
Hellowrold Interface:
Package Com.tgb.ejb;public interface HelloWorld {public string SayHello (string name);}
Hellowroldbean Implementation class:
Package Com.tgb.ejb;import javax.ejb.remote;import javax.ejb.stateless;/** * Session Bean Implementation class HelloWorld */@Stateless @remote ({helloworld.class}) public class Helloworldbean implements HelloWorld {@Overridepublic String SayHello (string name) {return name+ "Welcome to use ejb3.0~~";}}
3. Deployment
There are two ways of deploying:
① released in Eclipse
In the previously created server, right-click Add and Remove, and then start the project.
② is packaged and then deployed to the specified server
On the project right → Export→ EJB JAR file, select the JBOSS Server deployment directory: Jboss_home/server/defau Lt/deploy, complete.
4. Create a client
① Click File→new→other ... →java Project
② Add a reference to the above Ejb_01.jar and the JBoss Client jar package ( $JBOSS _home/clent/jbossall-clent.jar), add to the classpath path.
③ Write the client test class, the code is as follows
Package Com.tgb.ejb.client;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Com.tgb.ejb.helloworld;public class Client {public static void main (string[] args) throws Namingexception { InitialContext InitialContext = new InitialContext (); HelloWorld EJB = (HelloWorld) initialcontext.lookup ("Helloworldbean/remote"); String name = Ejb.sayhello ("Zhoujiang"); SYSTEM.OUT.PRINTLN (name);}}
5. Running the client
Start JBoss first;
Run the client code to prove our remote call succeeded.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
EJB Learning (ii)--Creating the first EJB project with Eclipse+jboss