This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dependency Injection Failure can't figured out the reason.  RSS feed

Chris Logan
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!