| EJB injection in Servlet fixed in JBoss AS 5 |
| Written by F.Marchioni | |||||
|
Supposing you have an EJB named "FilterEJB" :
@Stateless(name="FilterEJB")
@LocalBinding(jndiBinding="FilterEJB")
public class FilterManagerBean implements FilterManager {
.....
}
This EJB consists of a Stateless Session Bean which is bound into the JNDI context "FilterEJB"In order to inject this EJB into the Servlet, without the need to code the JNDI lookup and its caching, all you have to do is adding the @javax.ejb.EJB annotation: Important note: this is not fully portable to other AS since on other Application Server, like Glassfish the parameter "mappedName" is replaced by the standard "name".
public class ServletClient extends HttpServlet {
/**
* Injecting the EJB
*/
@EJB(mappedName = "FilterEJB")
private FilterManager ejbFilter;
}
if you prefer you can use the setter injection which gives you more control on your environment:
private FilterManager ejbFilter;
@EJB(mappedName = "FilterEJB")
public void setFilterManager(FilterManager ejbFilter)
{
this.ejbFilter = ejbFilter;
}
If you are running an earlier version of JBoss there's a workaround to get it working also there by setting CallByValue=true in the Naming service (jboss-service.xml)
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |


