I am somewhat new to Java EE (dependency injection) and I can't figure out why @Inject is giving me null, yet InitialContext.doLookup does work.
Here is my bean. It is just a DAO. A wrapper for EntityManager basically
@Stateless
public class PersonManager {
@PersistenceContext("unitName="PersonData")
EntityManager em;
...
}
Here is a REST service, where I am trying to utilize PersonManager:
@Path("/PersonService")
@RequestScoped
public class PersonService {
@Inject private PersonManager manager; //this comes up null
@GET
@Produces("text/html")
public String getAllPersons() {
List<Person> personList manager.findAll(); //null pointer exception, manager null
}
}
Now what is weird is, if I do a lookup on PersonManager, it does work, like this:
@GET
@Produces("text/html")
public String getAllPersons() {
try {
manager = InitialContext.doLookup("java:global/PersonApp/PersonData/PersonManager");
}
catch(Exception e) {
e.printStackTrace();
}
List<Person> personList manager.findAll(); //this works!
}
Any idea why @Inject doesn't work here? I am using an EAR with a WAR and JAR within it like this:
EAR (PersonApp)
--JAR (PersonData - ejb module - contains PersonManager)
--WAR (PersonRest - web module - contains PersonService)