Home Web interfaces EJB injection in Servlet fixed in JBoss AS 5
12 | 03 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Login
Sign here for the NewsLetter.



Poll
What book could be in your wish list next XMas ?
 
JBoss admin resources
Banner
JBoss howto

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More

How do I configure a Queue/Topic to work in a cluster?

JBoss recipe of the day ...
Read More
EJB injection in Servlet fixed in JBoss AS 5
Written by F.Marchioni   
This mini tutorial covers "The curious Case of EJB injection into Servlets".  Why curious ? because this was the only JEE dependency injection which gave some troubles to developers...until  JBoss AS 5.0 finally fixed JBAS-5646 The EJB 3.0 Specification - JSR 220 - gives developers a lot of flexibility in where to specify resource dependencies and how those resources can be deployed on the Java EE 5 platform. One useful kind of injection is the EJB injection into Servlets.

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
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."