To access a stateless session bean to a POJO (SLSB), it is no way the session bean via @ EJB annotation to inject a la. The injection works only with managed classes (Session Beans or Servlets, for example). There
similar problems also occur with a "managed" JSF Managed Bean (JSF2.0 when using the View-Scopes (@ view scoped) and the annotation @ EJB).
This further helps the Service Locator pattern (see
http://mr678.blogspot.com/2008/10/ejb3-und-servicelocator.html
). With the help of this pattern is a session bean locates.
In my applications, I would often use by local interface within the same application (WAR or EAR file) a SLSB. So far, access via JNDI to various app servers was different. The thing that bothered me so far because I use the name of the EAR file is encoded always hard.
The JEE6 Spec (EJB3.1) there is only one standardized access. Http://blogs.sun.com/MaheshKannan details on this in a blog by Mahesh Kannan of sun (see / entry / portable_global_jndi_names
) Abstract:
accessed within the same application is as follows:
EAR File: java: app / \u0026lt;module-name> / \u0026lt;bean-name>! \u0026lt;fully-qualified-intf-name> or WAR File: java: modules / \u0026lt;bean-name> \u0026lt;fully-qualified-intf-name>
After I currently small application on the AppServer only build a WAR module, require . If, in this case, no hard-coded modules or application with more
(Adam Bien has this very interesting blog entries on the subject applications as a WAR Module Deploying; http://www.adam-bien .com/roller/abien/entry/is_java_ee_6_war ; http://www.adam-bien.com/roller/abien/entry/java_ee_6_kills_the )
for my service locator class to access on SLSBs within my WAR file therefore looks quite simply:
@SuppressWarnings( "unchecked" )
public static
<T> T getService(Class<T> clazz) {
try
{ InitialContext ctx = new InitialContext();
String lookupPath =
"java:module/"
+ clazz.getSimpleName() + "!" + clazz.getName();
return
(T) ctx.lookup(lookupPath);
} catch
(NamingException e) { throw new
ServiceLocatorException(e); }
}
0 comments:
Post a Comment