File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Dependency Injection Failure can't figured out the reason. 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 "Dependency Injection Failure can Watch "Dependency Injection Failure can New topic

Dependency Injection Failure can't figured out the reason.

Chris Logan

Joined: Jan 31, 2011
Posts: 10
Hello everyone!

I'm still fighting with EJB 3.0 and today I got an error connected with dependency injection. Namely I was trying to inject stateless session bean called ObslugaPracownika to the other stateless session bean called ObslugaKlienta using the @EJB annotation. I'm going to write what I did and what kind of error occurred.

Bacis information:

EJB 3.0 implementation: JBoss Applicaiton Server
Component using the injection: @Stateless(name="ObslugaKlientaSLSBean") komponenty.ObslugaKlienta implements ObslugaKlientaRemote
Injected component: @Stateless komponenty.ObslugaPracownika implements ObslugaPracownikaRemote, ObslugaPracownikaLocal

First attempt:

Note: Injected component wasn't registered in JNDI ENC.

Server's console:

Deployment "jboss.j2ee:ear=EJBSklepEAR.ear,jar=EJBSklep.jar,name=ObslugaKlientaSLSBean,service=EJB3" is missing the following dependencies:
Dependency "<UNKNOWN jboss.j2ee:ear=EJBSklepEAR.ear,jar=EJBSklep.jar,name=ObslugaKlientaSLSBean,service=EJB3>" (should be in state "Described", but is actually in state "** UNRESOLVED Demands 'Class:komponenty.ObslugaPracownika' **")

Deployment "<UNKNOWN jboss.j2ee:ear=EJBSklepEAR.ear,jar=EJBSklep.jar,name=ObslugaKlientaSLSBean,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'Class:komponenty.ObslugaPracownika' **

Second attempt:

Note: Injected component was registered in JNDI ENC like this

(fragment of java:comp namespace of ObslugaKlienta component)
+- obslugaPracownikaSLSBean[link -> EJBSklepEAR/ObslugaPracownika] (class: javax.naming.LinkRef)

(fragment of global namespace)
+- EJBSklepEAR (class: org.jnp.interfaces.NamingContext)
| +- ObslugaPracownika (class: org.jnp.interfaces.NamingContext)
| | +- local (class: Proxy for: komponenty.ObslugaPracownikaLocal)
| | +- local-komponenty.ObslugaPracownikaLocal (class: Proxy for: komponenty.ObslugaPracownikaLocal)
| | +- remote-komponenty.ObslugaPracownikaRemote (class: Proxy for: komponenty.ObslugaPracownikaRemote)
| | +- remote (class: Proxy for: komponenty.ObslugaPracownikaRemote)

... but when I tried to get reference to the ObslugaKlienta component in client app I had following errors:

Client side:

Exception in thread "main" javax.ejb.EJBException: java.lang.IllegalArgumentException: failed to inject org.jnp.interfaces.NamingContext@c1c084f (implements [interface javax.naming.event.EventContext, interface]) from env/obslugaPracownikaSLSBean (link -> EJBSklepEAR/ObslugaPracownika) into komponenty.ObslugaKlienta.obslugaPrac of komponenty.ObslugaKlienta@74ff364a

Server side:

14:18:44,650 ERROR [FieldBeanProperty] Field Classloader: BaseClassLoader@5fe8bb4c{vfszip:/D:/Programowanie/JAVA/JBOSS/jboss-5.0.1.GA/server/default/deploy/EJBSklepEAR.ear/}
Value ClassLoader: BaseClassLoader@7b6bbeaf{vfsfile:/D:/Programowanie/JAVA/JBOSS/jboss-5.0.1.GA/server/default/conf/jboss-service.xml}
Equal Loaders: false
14:18:44,651 ERROR [FieldBeanProperty] failed to set value org.jnp.interfaces.NamingContext@38b7f14b on field komponenty.ObslugaPracownika komponenty.ObslugaKlienta.obslugaPrac; Reason: ClassLoaders of value and target are not equal
java.lang.IllegalArgumentException: Can not set komponenty.ObslugaPracownika field komponenty.ObslugaKlienta.obslugaPrac to org.jnp.interfaces.NamingContext

Do you maybe know how to solve the problem? Can you help me?

Chris Logan

Joined: Jan 31, 2011
Posts: 10
I solved the problem. I thought that I wrote the code like I read in the book but I made a mistake and instead of declaring the appropriate interface (local / remote) for injected bean I declared the object member of injected bean.

Simple mistake:

@EJB private ObslugaPracownika obslugaPrac -> @EJB private ObslugaPracownikaRemote obslugaPrac;
I agree. Here's the link:
subject: Dependency Injection Failure can't figured out the reason.
jQuery in Action, 3rd edition