aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Injection of EJB in POJO class not working.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Injection of EJB in POJO class not working.." Watch "Injection of EJB in POJO class not working.." New topic
Author

Injection of EJB in POJO class not working..

Yash Don
Greenhorn

Joined: May 19, 2010
Posts: 12
Hi ,

I am trying to inject Stateless session bean in POJO(non-EJB) class using @EJB annotation but I am getting NPE exception but same is working inside EJB.

So my question is can we inject EJB inside POJOs?

THanks,
Yash


Regards,
Yash

SCJP, SCWCD
Panos Gouleas
Greenhorn

Joined: May 10, 2011
Posts: 4
Hello,

i have a similar problem since I am trying to inject a ConnectionFactory using @Resource annotation, but I am getting null result (no error, just my object is null).
I am trying this in a jsf managed bean. Is this possible or do I have to do it in another way?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30916
    
158

Panos,
Welcome to CodeRanch! What version of JEE are you using? (Injecting into a managed bean should work in the latest version.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Panos Gouleas
Greenhorn

Joined: May 10, 2011
Posts: 4
Eclipse Helios with jdk 1.6
Jboss 6 final
JSF 2.0
Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

Hi Panos,

You have to add your EJB servers library files and JavaEE API libraries into your projects library directory to run that program outside from the EJB server.
But I suggest to use JNDI lookup for get server side instance if your client program runs outside from the EJB server.
Akshay Sahu
Greenhorn

Joined: Sep 04, 2009
Posts: 26

Hi,

Injection of an enterprise bean in a non-managed POJO is not possible.

If you're trying for something say

@EJB
private SampleSessionLocal sessionLocal;


in a simple java class, which comes after your servlet takes the input from the client,
then the container would obviously throw a Null Pointer Exception.

As far as I know, an enterprise bean can be injected into the following managed java objects:
1. Another Enterprise Bean ( EJB 2 or EJB 3 compatible).
2. A Servlet
3. JSF class.

I am not sure about the JSF thing, but the first two things are for sure.

Regards,
Akshay Sahu

Akshay Sahu
OCBCD (EJB 3), OCJP (Java SE 6), MCTS (MS SQL Server 2005)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Injection of EJB in POJO class not working..