wood burning stoves 2.0*
The moose likes JBoss/WildFly and the fly likes @Resource annotation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "@Resource annotation question" Watch "@Resource annotation question" New topic
Author

@Resource annotation question

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hi,

I am using @Resource annotation for injecting the data source as below:

But, I get a Null Pointer Exception.

If i do a JNDI look up like this it works



What am i doing wrong with injecting the data source?

Thanks,
Srikanth.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9929
    
158

Which version of JBoss and Java do you use? And where are you injecting this resource?


[My Blog] [JavaRanch Journal]
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
Which version of JBoss and Java do you use? And where are you injecting this resource?


I am using JBoss 4.2.3GA with java 6. I am trying to inject the resource in a servlet at class level.
[ December 04, 2008: Message edited by: Srikanth Reddy ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9929
    
158

What does your web.xml look like? Have you used the web-app 2.5 xsd in the web.xml?
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
What does your web.xml look like? Have you used the web-app 2.5 xsd in the web.xml?


Is it mandatory to use web-app 2.5 xsd? Also do i need to add <resource-ref> element in the web.xml?
[ December 04, 2008: Message edited by: Srikanth Reddy ]
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
What does your web.xml look like? Have you used the web-app 2.5 xsd in the web.xml?


Jaikiran,

Here is how my web.xml looks like:


I am trying to inject the resource in servlet as:


Do i need to add resource-ref element in web.xml?

Thanks,
Srikanth.
[ December 04, 2008: Message edited by: Srikanth Reddy ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9929
    
158

No you don't need a resource-ref in the web.xml
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9929
    
158

Try this:



Assuming that you are deploying a -ds.xml file to bind the OracleDS.
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
Try this:



Assuming that you are deploying a -ds.xml file to bind the OracleDS.


Jaikiran,

I already did what you said with no luck. I spent whole day on this issue but didn't get any solutions.

Today i downloaded JBoss 5.0.0GA amazingly the same code worked!

I thought @resource annotations work in JBoss 4.2.3 but now i think they only work with JBoss 5.x versions. Is that correct?

Thanks,
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9929
    
158

Originally posted by Srikanth Reddy:
Jaikiran,

I already did what you said with no luck. I spent whole day on this issue but didn't get any solutions.

Today i downloaded JBoss 5.0.0GA amazingly the same code worked!

I thought @resource annotations work in JBoss 4.2.3 but now i think they only work with JBoss 5.x versions. Is that correct?



I did know that @EJB injection would not work in JBoss-4.2.x, but i thought the @Resource would work. Infact, i think i even had an example on my previous machine with JBoss-4.2.3 where it had worked (if i remember correct). Once i get my new machine setup properly, let me give this a try.

Good to know, it worked with JBoss-5 GA which got released today.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @Resource annotation question
 
Similar Threads
Spring 2.5 / AbstractTransactionalJUnit4SpringContextTests/ datasource
JBoss Oracle DataSource Configuration
DI with @Resource adnotation understanding
ORACLE XA DataSource Problem in WebSphere 5.1
Problem with ORM by Weblogic