In EJB3.0, use another bean in one bean

Source: Internet
Author: User
Tags prepare jboss jboss server



Interface Helloworld.java
Package Jcuckoo; Public interface HelloWorld {public string SayHello (string name);} Interface Other.java
Package Jcuckoo; Public interface Other {public String sayme ();} Local interface Helloworldlocal.java
Package Jcuckoo; Public interface helloworldlocal extends helloworld{} Implement class Otherbean.java
Package Jcuckoo.impl; Import javax.ejb.Local; Import javax.ejb.Stateless; Import Jcuckoo.other; @Stateless @Local (Other.class)//The default annotation is the local public class Otherbean implements the other {public String Sayme () {R     Eturn "Other"; } } Implement class Helloworldbean.java
Method One: Lookup implementation through Jndi call another bean
@Stateless//without the following Remote and local annotations, the default is the local @Remote (Helloworld.class) @Local (helloworldlocal.class) public  class helloworldbean implements helloworld,helloworldlocal {    public  string sayhello (string name)  {                  //Lookup via Jndi         try  {            initialcontext ctx =  new initialcontext ();             other other= (Other) ctx.lookup (" Otherbean/local ");             return name+ "said: Hello," +other.sayMe () ;         } catch  (namingexception e)  {             e.printstackTrace ();         }         return  Null             &nbsp} Method Two: Calling another bean through an EJB annotation implementation
@Stateless//without the following Remote and local annotations, the default is local @Remote (helloworld.class) @Local (helloworldlocal.class) public class Helloworldbean implements Helloworld,helloworldlocal {//@EJB other other;//lookup by injection///If two of the same type of other is found when injected, it will be reported     Wrong. This is the time to specify the corresponding beanname, so even if there are multiple classes to implement the other interface, there will be no problem @EJB (beanname= "Otherbean") other;     Public String SayHello (string name) {return name+ said: "Hello," +other.sayme (); }} Other Annotations:

Timerservice is a known service in EJB3 and can be annotated with resource
@Resource Timerservice Timerservice;

Specifies that the data source is also annotated by resource and that the corresponding Jndi name is specified through the parameter mappedname
@Resource (mappedname= "") DataSource DataSource;

published, there are several ways to use Ant publishing here:
<?xml version= "1.0"  encoding= "UTF-8"?> <project basedir= "."   name= "Ejbtest" >     <property name= "Src.dir"  value= "${basedir}/" src "/>     <property environment=" env "/>     <property  name= "Jboss.home"  value= "${env. Jboss_home} "/>     <property name=" jboss.server.config " value=" Default "/ >     <property name= "Build.dir"  value= "${basedir}/build"/>           <path id= "Build.classpath" >         <fileset dir= "${jboss.home}/client" >         < Include name= "*.jar"/>        </fileset>         <pathelement location= "${build.dir}"/>     </path>     <target name= "Prepare" >          <delete dir= "${build.dir}"/>         <mkdir dir= "${ Build.dir} "/>     </target>     <target name=" Complie "  depends= "Prepare"  description= "compiling" >         <javac  srcdir= "${src.dir}"  destdir= "${build.dir}" >              <classpath refid= "Build.classpath"/>          </javac>     </target>     <target name= "Ejbjar"  depends= "Complie"  description= "Create EJB Publishing Package" >          <jar destfile= "${basedir}/${ant.project.name}.jar" >               <fileset dir= "${build.dir}" >                  <include name= "**/*.class"/>              </fileset>          </jar>     </target>     <target name= "Deploy"  depends= "Ejbjar"  description= "Publish EJB" >         <copy  file= "${basedir}/${ant.project.name}.jar"  todir= "${jboss.home}/server/${jboss.server.config}/deploy "></copy>     </target>     <target name=" Undeplay " description=" Uninstall EJB ">         <delete file=" ${ Jboss.home}/server/${jboss.server.config}/deploy/${ant.project.name}.jar "></delete>      </target> &Lt;/project> start the JBoss server.

Next build the client Ejbclient.java
package jcuckoo.client; import jcuckoo.helloworld; import javax.naming.initialcontext; import javax.naming.namingexception; public class ejbclient {    public static void main (String[)  args)  {        try {             initialcontext ctx=new initialcontext ();             helloworld helloworld= (HelloWorld ) Ctx.lookup ("Helloworldbean/remote");             system.out.println (Helloworld.sayHello ("Remote Wheat"));         } catch  (namingexception e)  {             system.out.println (E.getMessage ());         }    &NBSP}}, you can see the corresponding result:
Remote wheat said: Hello, other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.