开发者

EJB invoke a method which later invoke another EJB -> return null

开发者 https://www.devze.com 2023-03-14 06:04 出处:网络
I have a structure like this @Stateless public class CoreMainEJB implements CoreMainEJBRemote, CoreMainEJBLocal {

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消