Dependency Injection Failure can't figured out the reason


EJB programming & troubleshooting: Dependency Injection Failure can't figured out the reason

  1. 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:
    package komponenty;

    public class ObslugaKlienta implements ObslugaKlientaRemote {

    @EJB private ObslugaPracownika obslugaPrac;


    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:
    package komponenty;

    public class ObslugaKlienta implements ObslugaKlientaRemote {
        private ObslugaPracownika obslugaPrac;

    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?

  2. The first attempt was right except for the fact that you should use the local interface at the injection point:

    @EJB private ObslugaPracownikaLocal obslugaPrac;

  3. Local interface[ Go to top ]

    Yes local interface should sort the issue



    agile development methodology