Ejb3.0 Development Guide: stateless Session Bean You do not need to implement the home interface for ejb3.0 defined in all ejb3.0 specifications. A session bean must have a service interface, which is implemented by the Session Bean or can be generated by the Session Bean. In this way, you can write only one file to generate business logic implementation classes, remote interfaces, and local interfaces. ... @ Remote @ Local @ Stateless Public class counterbean { ...... } In the current JBoss implementation, there must be an independent business interface. This interface does not need to implement ejbobject or ejblocalobject. A stateless Session Bean must use the stateless annotation to indicate that it is a stateless Session Bean. The EJB container determines its type based on this annotation. Or implement the javax. EJB. sessionbean interface. A stateless session bean can implement the setsessioncontext method or not. A stateless session bean can implement the ejbcreate/ejbremove method. A stateless session bean can pass dependency injection (dependency injection) That is, IOC, which is currently quite popular, obtains the resource and environment attributes of the container. For more information, see the following sections. Import the example stateless provided in this article in eclipse. This is an example of a counter. It implements two business methods: Add and getnumber. The add method adds an integer to the counter and the getnumber method returns the current value of the counter. This example mainly contains five files: Counter. Java: business interface. Counterbean. Java: Business implementation class. In the future, the EJB we developed will also be named like this (add bean to the interface name ). Client. Java: client class for testing EJB. JNDI. properties: The JNDI property file that provides basic configuration properties for accessing jdni. Build. xml: ant configuration file for compiling, publishing, testing, and clearing ejbs. The following describes 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. It defines the two business methods described above. At the same time, we add remote annotations to this interface so that our remote client can find it by using the JNDI name, and call its business method. How can I configure its JNDI name? You do not need to configure its JNDI name or write its configuration file. In ejb3.0 implemented by JBoss, you do not have to write any EJB deployment files or JBoss deployment files. By default, JBoss uses the full name of the interface as its JNDI name. In the above example, its full name is: Com. kuaff. ejb3.stateless. counter, you can also get it through counter. Class. forname. Counterbean. Java Package com. kuaff. ejb3.stateless; Import javax. EJB. stateless; @ Stateless Public class counterbean implements counter { Private int number = 0; // Add I to the counter Public int add (int I) { Number + = I; Return number; } // Obtain the current count Public int getnumber () { Return number; } } This is the counter implementation class. Note that this class uses stateless for comments, which is required. 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) 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 released. First pass CTX = new initialcontext (); obtain the context, search for the counter through lookup, and add 10 to the counter to 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 configures the attributes required for the JNDI operation. Because we are testing the native EJB service, we do not have to modify the above attributes. Build. xml <Project name = "kuaff_ejb3_tutorial" default = "ejbjar" basedir = "."> <Property environment = "env"/> <Property name = "src. dir" value = "$ {basedir}/src"/> <Property name = "JBoss. Home" value = "$ {env. jboss_home}"/> <Property name = "build. dir" value = "$ {basedir}/build"/> <Property name = "build. classes. dir" value = "$ {build. dir}/classes"/> <Path id = "classpath"> <Fileset dir = "$ {JBoss. Home}/lib"> <Include name = "**/*. Jar"/> </Fileset> <Fileset dir = "$ {JBoss. Home}/Server/All/lib"> <Include name = "**/*. Jar"/> </Fileset> <Fileset dir = "$ {JBoss. Home}/Server/All/deploy/ejb3.deployer"> <Include name = "*. Jar"/> </Fileset> <Fileset dir = "$ {JBoss. Home}/Server/All/deploy/jboss-aop.deployer"> <Include name = "*. Jar"/> </Fileset> <Pathelement location = "$ {build. classes. dir}"/> <Pathelement location = "$ {basedir}"/> </Path> <Property name = "build. classpath" refID = "classpath"/> <Target name = "prepare"> <Mkdir dir = "$ {build. dir}"/> <Mkdir dir = "$ {build. classes. dir}"/> </Target> <Target name = "compile" depends = "prepare"> <Javac srcdir = "$ {SRC. dir }" Destdir = "$ {build. classes. dir }" DEBUG = "on" Deprecation = "on" Optimize = "off" Supported des = "**"> <Classpath refID = "classpath"/> </Javac> </Target> <Target name = "ejbjar" depends = "compile"> <Jar jarfile = "build/statelesssample. ejb3"> <Fileset dir = "$ {build. classes. dir}"> <Include name = "**/*. Class"/> </Fileset> </Jar> <Copy file = "build/statelesssample. ejb3" todir = "$ {JBoss. Home}/Server/All/deploy"/> </Target> <Target name = "run" depends = "ejbjar"> <Java classname = "com. kuaff. ejb3.stateless. Client" fork = "yes" dir = "."> <Classpath refID = "classpath"/> </Java> </Target> <Target name = "clean. DB"> <Delete dir = "$ {JBoss. Home}/Server/All/data/hypersonic"/> </Target> <Target name = "clean"> <Delete dir = "$ {build. dir}"/> <Delete file = "$ {JBoss. Home}/Server/All/deploy/statelesssample. ejb3"/> </Target> </Project> Run run. BAT: Run? In the {$ jboss_home}/bin directory? C All: Start JBoss. Execute ejbjar target in the ant view of Eclipse. Alternatively, go to the project directory under the command line and execute ant ejbjar to package the compilation and release the EJB. Run target in the ant view of Eclipse. Or enter the project directory under the command line and run ant run to test the EJB. |