First, why?
When we call the server object on the client side, we usually need to find the session bean or MDB through JNDI of the servers. JNDI Lookup is a key step to decoupling the client from the actual server. However, it is not elegant to use a string directly for JNDI lookups. There are several reasons for this:
• The client and server must have a consistent string-based name. It is not certified at compile time or is checked at deployment time.
· The type of the service object returned from JNDI is not checked at compile time, and a conversion (casting) error may occur at run time.
· The lengthy lookup code, with its own try-catch code block, is a repetitive and messy EJB 3.0 between applications, providing a simple and elegant way to decouple service objects and resources from any POJO.
For these reasons, we have an EJB dependency injection.
Second, how?
1. Inject Session Bean
There are four ways to inject session beans:
[1] Beaninterface: Refers to the interface implemented by the injected attribute EJB.
[2] Name: The name of the injected bean registered in Jndi ENC, different servers have different differences.
[3] Beanname: Specifies the name of the EJB (if @stateless or @stateful has not set the Name property, the default is the class name without the package name)
[4] Mappedname: The jndi name of the EJB being injected, different servers have different differences.
Example: through Beanname injection
① local interface and implementation classes:
Local interface public interface HelloWorld {public string SayHello (string name);} Local interface Implementation bean@stateless (name= "HelloWorld") @Local ({Helloworld.class}) public class Helloworldbean implements HelloWorld {@Overridepublic string SayHello (string name) {return name+ "Welcome to Ejb3.0~~";}}
② Remote interface and implementation class:
Remote interface public interface Callhello {public string SayHello (string name);} Remote interface Implementation class @stateless@remote ({Callhello.class}) public class Callhellobean implements Callhello {@EJB (name= "HelloWorld ")//@EJB (mappedname=" helloworld/local ") private HelloWorld HelloWorld; @Overridepublic string SayHello (string name) { String result = Helloworld.sayhello ("Jiang"); SYSTEM.OUT.PRINTLN ("EJB injection succeeded:" +result); return result;}}
③ Client Calls:
public class Client {public static void main (string[] args) throws Namingexception {InitialContext InitialContext = new in Itialcontext (); Callhello EJB = (Callhello) initialcontext.lookup ("Callhellobean/remote"); String name = Ejb.sayhello ("Zhoujiang"); SYSTEM.OUT.PRINTLN (name);}}
Note: Annotations can also be used in JavaBean style setter methods, with the same effect. Another thing is that JavaBean's @stateful or @stateless annotation's "mappedname" attribute can change the jndi name of JavaBean, such as @stateful (mappedname= "123"), So this is the case with Jndi calls:
Callhello EJB = (Callhello) initialcontext.lookup ("123");
2. Inject Resource
@EJB annotations can only inject EJB stub objects, EJB 3.0 supports @resource annotations in addition to @ejb annotations to inject any
Resources. The following example shows how to inject a data source. "Java:/defaultmysqlds" is the global Jndi name of the data source Defaultmysqlds.
@Stateless @remote ({injection. Class}) public class Injectionbean implements injection {@EJB (beanname = "Helloworldbean" ) HelloWorld HelloWorld; @Resource (mappedname = "Java:/defaultmysqlds") DataSource Mydb;public string SayHello () {string str = ""; try {Connection conn = mydb.getconnection (); Statement stmt = Conn.createstatement (); ResultSet rs = stmt.executequery ("Select Studentname from Student"), if (Rs.next ()) {str = rs.getstring (1);} Rs.close (); Stmt.close ();} catch (SQLException e) {e.printstacktrace ();} return HelloWorld. SayHello (str);}}
Summary: Just beginning to learn EJB injection is the time, by a few name, Mappedname, Beanname confused, and then on their own to knock a demo, in practice to verify their ideas, so often than just stay in theory to understand the profound.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
EJB Learning (v)--Dependency Injection