EJBs defined in all EJB3.0 specification do not have to implement the HOME interface.
A session bean must have a business interface, which is implemented by a session bean or can be generated by a session bean. In this way, you can only write a file, you can generate business logic implementation classes, remote interfaces, local interfaces, etc.
... @Remote @Local @StateLess Public Class Counterbean {...}
In the current JBoss implementation, there must be a separate business interface.
This interface does not have to implement EJBOBJECT or EJBLOCALOBJECT.
A stateless conversation bean must use Stateless annotations to indicate that it is a stateless session bean. The EJB container will determine its type according to this annotation. Or implement a javax.ejb.sessionBean interface.
A stateless session bean can implement the setSessionContext method or not.
A stateless session bean enables the EJBCREATE / EJBREMOVE method.
A stateless session bean can be injected into the inject (Dependency Injection)
), It is now noisy IOC to get the resources and environmental properties of the container. For details, please see the chapter behind.
The example stateless provided herein is imported in Eclipse.
This example is an example of a counter that implements two business methods add and getNumber. The Add method is to add an integer value to the counter, and the GetNumber method will get the current value of the counter.
This example has main five files:
Counter.java: Business Interface.
CounterBean.java: Business implementation class. The EJB we develop in the future is also named (adding beans on the interface name).
Client.java: Test EJB client class.
JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.
Build.xml: Ant profile, to compile, release, test, and clear EJB.
Here is a description of the content of each file.
Counter.java
Package com.kuaff.ejb3.stateless; import javax.ejb.remote; @Remote Public interface counter {public int Add (int i); public int getNumber ();}
This interface is very simple, defined the two business methods described above, and we add the Remote annotation for this interface so that our remote client can find it through the JNDI name, and call its business method.
How does its JNDI name configuration?
You don't have to configure its JNDI name, or you don't have to write its profile. In the EJB3.0 implemented in JBoss, you don't have to write any EJB deployment files and JBoss deployment files. JBoss defaults to use the full name of the interface as its JNDI name. In the example above, its full name is:
com.kuaff.ejb3.Stateless.counter, you can also get through counter.class.Forname ().
Counterbean.java
Package com.kuaff.ejb3.stateless; import javax.ejb.stateless; @StateLess Public Class CounterBeans counter {private int number = 0; // Add I public int Add (INT i) {Number = i; Return Number;} // Gets the current count public int GETNUMBER () {return number;}} This is the implementation class of the counter. Note that this class uses stateless to make a comment, which is necessary.
Client.java
package com.kuaff.ejb3.stateless; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client {public static void main (String [] args) {InitialContext ctx; try {ctx = new InitialContext (); Counter counter = (counter) ctx.lookup (counter.class.getname ()); counter.add (10); System.out.Println ("Current Number:" Counter.getNumber ()); counter.Add ( 10); System.out.Println ("Current Number:" Counter.getNumber ()); Counter Counter2 = (Counter) CTX.lookup (counter.class.getname ()); counter2.add (10); system .out.println ("Current Number:" Counter2.getNumber ());} catch (namingexception e) {E.PrintStackTrace ();}}}
This class is used to test the counter EJB we publish. First pass
CTX = new initialContext (); get the context, then use the Lookup lookup counter, then add 10 to the counter, display the current counter information.
JNDI.Properties
Java.naming.factory.initial = org.jnp.interfaces.namingcontextFactory java.naming.Factory.URL.PKGS = Org.jboss.naming: Org.jnp.interfaces java.naming.provider.URL = localhost
This file is configured with the properties required by JNDI operations because we are testing this EJB service, so it is not necessary to modify the above properties.
Build.xml
Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.
Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.