I have a structure like this
@Stateless
public class CoreMainEJB implements CoreMainEJBRemote, CoreMainEJBLocal {
@Override
public void process(String configFileName) throws Exception {
...
PackageProcessor p = new PackageProcessor();
p.processPackage(Object something);
}
}
then in PackageProcesor.java
-> is not annotate @Stateless
public class PackageProcessor(){
@EJB
private GenericEJB genericEJB;
public void processPackage(Object something){
genericEJB.create(something);
}
...
}
The i开发者_JAVA百科njection of GenericEJB
return null. Here is the content of GenericEJB
@Stateless
@LocalBean
public class GenericEJB{
@PersistenceContext(unitName = "someWebPUnit")
private EntityManager em;
public void create(Object t){
em.persist(t);
}
}
Any idea why the injection of GenericEJB
return null?
When you create an object like this:
PackageProcessor p = new PackageProcessor()
the EJB container does not know anything about it, the container does not manage the lifecycle of this object, it also cannot inject anything, wrap it in aspects, apply transaction behavior. Simply put: for you application server this object does not exist.
Of course if you annotate PackageProcessor
with @Stateless
and inject it as ordinary EJB, it will work. Any reasons why you don't want to do this?
精彩评论