Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dependency Injection not working?

 
vShyam Sundar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Dependency Injection in EJB3 is allowed only on manageable classes? What is meant by this?
I tried to execute program with JNDI lookup and same With Dependency Injection. Unfortunately the code with Dependency Injection doesn't work. When i tried to execute the program with DI. I got a null pointer exception

I have pasted the code below. Please provide your valuable inputs


and my business interface is:.


and stateless bean code is as follows:


The above code executed well but when i changed the action servlet to achieve dependency injection, I got the null pointer exception.




Thanks,
Shyam.
 
Murali Ranga
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jndi name is missing to annotation.
@Stateless(name="CalculatorBean", mappedName="Sample1/CalculatorBean/remote")

 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a web.xml? What does it look like? The xsd version declaration should be 2.5 or 3.0 for injection to work.
 
vShyam Sundar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks JaiKiran. It worked now. Previously i just had <web-app> tag in web.xml but now i replaced with

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">

Any idea on why we need this extra attributes?

and also Dependency Injection is allowed only on managable classes- What is meant by this?

Thanks,
Shyam.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vShyam Sundar wrote:
Any idea on why we need this extra attributes?

That's because prior to web-app version 2.5, dependency injection wasn't available.

vShyam Sundar wrote:
and also Dependency Injection is allowed only on managable classes- What is meant by this?



It means that dependency injection is supported on classes whose lifecycle is managed by the container(s). For example the servlets, EJBs etc...
 
vShyam Sundar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic