This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes Issues Injecting EJBs into JSF Backing Beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Issues Injecting EJBs into JSF Backing Beans" Watch "Issues Injecting EJBs into JSF Backing Beans" New topic
Author

Issues Injecting EJBs into JSF Backing Beans

John Glista
Greenhorn

Joined: Dec 05, 2011
Posts: 4
I've discovered a very weird issue and can not think of why this would be. In my project, I have a few different faces-config files. However, I've noticed that any managed beans that are defined in anything other than the default "faces-config.xml" are not getting EJB references injected into them when @EJB is used. When I move the <managed-bean> definition out of the other config file and into "faces-config.xml", the injection magically works. It's obvious that all the managed beans in the other config files work properly, as there would be other significant issues in the project if they did not. However, I'm the first one to use EJB injection on this project; all previous code uses a service lookup/locator class.

Can anyone think of why this may be?

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

Welcome to the JavaRanch, John!

I don't know what should make annotation-based specs differ from xml-based specs, but in general, injecting EJBs into JSF managed beans is a problem, because in the case of entity EJBs, there's not much ability as to what instance of the EJB you can inject. Session EJBs would be less of an issue that way, but still problematic.

In order to be able to inject a bean into a Managed Bean, that bean has to have a symbolic name within the EL namespace, since the actual injection is an EL expression, regardless of whether it's done via annotation or via XML. However, EJBs wouldn't by themselves possess such a name. In the case of Spring Framework beans, the Spring bean namespace is merged into the JSF EL namespace by means of a special bridging class that's specified in the faces-config.xml file. EJBs would need something equivalent.


Customer surveys are for companies who didn't pay proper attention to begin with.
John Glista
Greenhorn

Joined: Dec 05, 2011
Posts: 4
Thanks for the welcome, and thanks for the reply.

This is unfortunate news. I previously worked at a company where I was able to use Spring as much as I wanted, and lately moved to a company that uses only EJB, JPA/Hibernate, and JSP/JSF. Making the transition has been difficult, and the promise of using EJB injection was nice, but as you said, and as I've noticed, it seems a bit problematic. I just hate the idea of of using a "service locator" class and doing lookups. It's just one more interface/class to maintain, and very old-school.
John Glista
Greenhorn

Joined: Dec 05, 2011
Posts: 4
because in the case of entity EJBs, there's not much ability as to what instance of the EJB you can inject


I also have a question about that statement. I'm not sure what you mean by this, given that you can specify a mappedName on the @EJB annotation. Isn't it true that you aren't injecting an actual EJB instance, only a reference to a proxy that is managed by the container?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

The mappedName is a JNDI directory name and is not part of the EL namespace. Also, I don't think it works on Entity EJBs, only on Session EJBs.

There's no particular reason that a Session EJB couldn't be injected into a JSF Managed Bean - other than the fact that EJBs tend to be more mobile than JSF beans. But to do so, you'd need to catalog the session EJB into the JSF EL namespace. That would require an EL bridge (resolver) that could take a JSF namespace name and resolve it to a particular EJB instance. I don't know of any, myself, but then I use Spring Beans as functional replacements for session EJBs, since I don't have a requirement for the extra amenities (such as remote execution) that session EJBs offer.
John Glista
Greenhorn

Joined: Dec 05, 2011
Posts: 4
Luckily, I'm using only Session EJBs. I can see what you are saying about the EL bridge. There must already be something going on there, because as I said, I can successfully inject EJB references into any JSF managed-bean that is defined *only* in faces-config.xml, and not any other faces config file, such as faces-config-xxx.xml, faces-config-yyy.xml, etc. I'm curious to know what is responsible for allowing this. I wonder if it could be something specific with WebLogic's J2EE implementation. Either way, I'm a bit nervous to use it all, since other developers may see it being used, but not understand this strange restriction. On the other hand, I strongly dislike the idea of a service locator class. If it were up to me, I'd move over to Spring.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues Injecting EJBs into JSF Backing Beans
 
Similar Threads
JSF Session-scope managed beans and Multiple browsers
Session beans as backing beans ?
confusion regarding config files of admin and managed servers ---HELP PLEASE !!!!!
gettting managed bean names
Referencing a managed bean object inside another class