dependency injection in java class (neither EJB nor Servlet)

Discussions

EJB programming & troubleshooting: dependency injection in java class (neither EJB nor Servlet)

  1. Hi all.

    We are using WebSphere Applicacion Server, migrating from WAS 6.1 (EJB 2.1) to WAS 7 (EJB 3.0). We have a problem with dependency injection in a plain java class. 

    Our chain of invocations is:   Servlet --> EJB --> plain java class --> EJB (JPA class).
    We have put @EJB annotation inside de plain java class in order to access the JPA object, but our object is always null:
    public class MyClass1{

        @EJB private MyJPAClass1 jpa1;
        public void method1() {

                // here the object "jpa1" is null

        }

    }

    We've read that the dependency injection engine doesn't really inject anything in plain java classes (without considering main methods).Is that true? It doesn't seem a logical technical limitation (in my humble opinion).

    If it's true, is there a way to put that reference in the plain java class (without the need of introducing an EJB reference inside the EJB which is previous to our java class)?


    Thanks in advance.

  2. @EJB will only be injected to managed classes ie., application clients( with static main) servlets,stateful and stateless session beans. You didn't clearly mention use case but you can save servlet access jpa class for the data and so forth..  , otherwise in the above clas use JNDI lookup.