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  } 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;
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.