Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Injection of EJB in POJO class not working..

 
Yash Don
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Panos Gouleas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Panos,
Welcome to CodeRanch! What version of JEE are you using? (Injecting into a managed bean should work in the latest version.)
 
Panos Gouleas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse Helios with jdk 1.6
Jboss 6 final
JSF 2.0
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic