Obtain ejbcontext from EJB 3 in four ways

Source: Internet
Author: User
Original article address:Http://javahowto.blogspot.com/2006/06/4-ways-to-get-ejbcontext-in-ejb-3.html
1. Use the domain injection in the bean class. These domains can use any access modifier (e.g., private, public, protected, package default ).

Package com. Foo. EJB;

Import javax. annotation. resource;
Import javax. EJB. EJB;
Import javax. EJB. sessioncontext;
Import javax. EJB. stateless;

@ Stateless
Public class hellobean
Implements helloremote {
@ Resource
Private sessioncontext sCTX;

Public void Hello (){
System. Out. println ("sessioncontext from field injection:" + sCTX );
}
2. Use the Set Method in the bean class for injection. Method injection or domain injection can be used at the same time. These set methods can use any access modifier (e.g., private, public, protected, package default ).

@ Stateless
Public class hellobean
Implements com. Foo. EJB. helloremote {
Private sessioncontext sCTX;

@ Resource
Private void setsessioncontext (sessioncontext sCTX ){
This. sCTX = sCTX;
}
}

Or

@ Stateless
Public class hellobean implements com. Foo. EJB. helloremote {
Private sessioncontext sCTX;

@ Resource
Private void setsctx (sessioncontext sCTX ){
This. sCTX = sCTX;
}
}

3. Search for injected resources using the @ Resource Name attribute (if not specified, use the default name)

@ Stateless
Public class hellobean implements com. Foo. EJB. helloremote {
@ Resource (name = "sessioncontext ")
Private sessioncontext sCTX;

Public void Hello (){
Try {
Initialcontext Ic = new initialcontext ();
Sessioncontext sctxlookup =
(Sessioncontext) IC. Lookup ("Java: COMP/ENV/sessioncontext ");
System. Out. println ("Look up injected sCTX:" + sctxlookup );
} Catch (namingexception ex ){
Throw new illegalstateexception (Ex );
}
}

Or, if not specified, use the default name. Note: The default name of the injected resource is fully-qalified-class-name/variable-Name:

@ Stateless
Public class hellobean
Implements com. Foo. EJB. helloremote {
@ Resource
Private sessioncontext sCTX;

Public void Hello (){
Try {
Initialcontext Ic = new initialcontext ();
Sessioncontext sctxlookup =
(Sessioncontext) IC. Lookup ("Java: COMP/ENV/COM. Foo. EJB. hellobean/sCTX ");
System. Out. println ("Look up injected sCTX by default name:" + sctxlookup );
} Catch (namingexception ex ){
Throw new illegalstateexception (Ex );
}
}

4. Search for the standard name Java: COMP/ejbcontext (Note No/ENV)

@ Stateless
Public class hellobean implements com. Foo. EJB. helloremote {
Public void Hello (){
Try {
Initialcontext Ic = new initialcontext ();
Sessioncontext sctxlookup =
(Sessioncontext) IC. Lookup ("Java: COMP/ejbcontext ");
System. Out. println ("Look up ejbcontext by standard name:" + sctxlookup );
} Catch (namingexception ex ){
Throw new illegalstateexception (Ex );
}
}

The preceding example uses the stateless Session Bean, which can also be used for stateful Session Bean, Singleton Session Bean (introduced in EJB 3.1), and message-driven bean.
Ejbcontext is a specific type of resource, one that needs to be configured in the ejb-jar.xml and other vendor-specific configuration files (sun-ejb-jar.xml, JBoss. XML, etc ).
You can declare a reference to ejbcontext/sessioncontext/messagedrivencontext in the deployment description file, but javax. Naming. namenotfoundexception may be thrown.

These technologies are defined in ejb3 specifications and can run normally on all Java ee 5 compatible application servers. I have tested the above examples in javaee SDK 5/sjsas 9/glassfish.

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.