Ejb3.0 dependency Injection

Source: Internet
Author: User
You learned how to develop loose service components. however, to access those service objects, you need to use the server's JNDI to find the session bean or Message Queue (MDB ). JNDI lookup is a key step to decouple the client from the actual server. however, using a string directly for JNDI search is not elegant. there are several reasons for this:
The producer client and the server must have the same string-based name. It is not authenticated during compilation or is checked during deployment.
The type of the service object returned by the listener from the JNDI is not checked during compilation, and the conversion (casting) error may occur at runtime.
The pull code is lengthy and has its own try-catch code block, which is repetitive and messy between applications.
EJB 3.0 provides a simple and elegant method for any pojo to parse service objects and resources. with @ EJB annotation, You can inject the EJB stub object into any pojo managed by the EJB 3.0 container. if a comment is used on an attribute variable, the container will assign it a value before it is accessed for the first time. in the next version of JBoss, @ EJB comments are from javax. the annotation package is moved to javax. EJB.
The following example demonstrates how to inject the hloworldbean stateless Session Bean stub to the injectionbean class. Java code
  1. Injectionbean. Java
  2. Package com. foshanshop. ejb3.impl;
  3. Import com. foshanshop. ejb3.helloworld;
  4. Import com. foshanshop. ejb3.injection;
  5. Import javax. annotation. EJB;
  6. Import javax. EJB. Remote;
  7. Import javax. EJB. stateless;
  8. @ Stateless
  9. @ Remote ({injection. Class })
  10. Public class injectionbean implements injection {
  11. @ EJB (beanname = "helloworldbean ")
  12. Helloworld;
  13. Public String sayhello (){
  14. Return helloworld. sayhello (" ");
  15. }
  16. }

@ The beanname attribute in the EJB comment specifies the class name (without the package name) of the EJB, and its mappedname attribute specifies the JNDI name of the bean instance.
The following section demonstrates how to use the beanname or mappedname attribute to find the helloworldbean Session Bean Java code.

  1. Public class injectionbean implements injection {
  2. @ EJB (beanname = "helloworldbean ")
  3. // @ EJB (mappedname = "helloworldbean/remote ")
  4. Helloworld;
  5. ..

@ EJB annotation if it is used in the setter method of the JavaBean style, the container automatically uses the correct parameter call before the attribute is used for the first time.
Use the setter method of bean. The following snippet demonstrates how Java code is implemented.

  1. Public class injectionbean implements injection {
  2. Helloworld;
  3. @ EJB (beanname = "helloworldbean ")
  4. Public void sethelloworld (helloworld ){
  5. This. helloworld = helloworld;
  6. }
  7. ..

@ EJB annotation if it is used in the setter method of the JavaBean style, the container automatically uses the correct parameter call before the attribute is used for the first time.
Use the setter method of bean. The following section demonstrates how this is done.
Public class injectionbean implements injection {
Helloworld;
@ EJB (beanname = "helloworldbean ")
Public void sethelloworld (helloworld ){
This. helloworld = helloworld;
}
.....
The following is the remote service interface of injectionbean.
Injection. Java
// Author: lihuoming
Package com. foshanshop. ejb3;
Public interface injection {
Public String sayhello ();
}
The following is the JSP client code of Session Bean:
JBoss ejb3.0 instance tutorial
Copyright: Li fuming
Injectiontest. jsp
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "com. foshanshop. ejb3.injection, javax. Naming. *, java. util. properties" %>
<%
Properties props = new properties ();
Props. setproperty ("Java. Naming. Factory. Initial ",
"Org. jnp. Interfaces. namingcontextfactory ");
Props. setproperty ("Java. Naming. provider. url", "localhost: 1099 ");
Props. setproperty ("Java. Naming. Factory. url. pkgs", "org. JBoss. Naming ");
Initialcontext CTX;
Try {
CTX = new initialcontext (props );
Injection injection = (injection) CTX. Lookup ("injectionbean/remote ");
Out. println (injection. sayhello ());
} Catch (namingexception e ){
Out. println (E. getmessage ());
}
%>
@ EJB annotation can only inject EJB stub objects. In addition to @ EJB annotation, EJB 3.0 also supports @ resource annotation to inject any
Resources. The following example shows how to inject data sources. "Java:/defaultmysqlds" is the global JNDI name of defamymysqlds.
For data source configuration, see "JBoss data source configuration"
// Author: lihuoming
Package com. foshanshop. ejb3.impl;
Import java. SQL. connection;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import com. foshanshop. ejb3.helloworld;
Import com. foshanshop. ejb3.injection;
Import javax. annotation. EJB;
Import javax. annotation. resource;
Import javax. EJB. Remote;
Import javax. EJB. stateless;
Import javax. SQL. datasource;
@ Stateless
@ Remote ({injection. Class })
Public class injectionbean implements injection {
@ EJB (beanname = "helloworldbean ")
Helloworld;
@ Resource (mappedname = "Java:/defaultmysqlds ")
JBoss ejb3.0 instance tutorial
Copyright: Li fuming
Datasource mydb;
Public String sayhello (){
String STR = "";
Try {
Connection conn = mydb. getconnection ();
Statement stmt = conn. createstatement ();
Resultset rs = stmt.exe cutequery ("select studentname from student ");
If (Rs. Next ()){
STR = Rs. getstring (1 );
}
Rs. Close ();
Stmt. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return helloworld. sayhello (STR );
}
}
If the JNDI object is in the local (Java: COMP/ENV) JNDI directory, you only need to specify its name without a prefix. For example, note
Input a message connection factory and a messaging queue
@ Resource (mappedname = "connectionfactory ")
Queueconnectionfactory factory;
@ Resource (mappedname = "queue/")
Queue queue;
For the "well-known" object, @ resource annotation can inject them without specifying the JNDI name, and obtain its JNDI through the variable type.
Name. The following are some examples.
@ Resource
Timerservice TMS;
@ Resource
Sessioncontext CTX;
Similar to @ EJB annotations, @ resource annotations can also be used in the setter method of the JavaBean style.

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.