EJB 调用一个方法,该方法随后调用另一个 EJB ->返回空值
有一个这样的结构
@Stateless
public class CoreMainEJB implements CoreMainEJBRemote, CoreMainEJBLocal {
@Override
public void process(String configFileName) throws Exception {
...
PackageProcessor p = new PackageProcessor();
p.processPackage(Object something);
}
}
我在 PackageProcesor.java
-> 中 不是注解 @Stateless
public class PackageProcessor(){
@EJB
private GenericEJB genericEJB;
public void processPackage(Object something){
genericEJB.create(something);
}
...
}
的注入 GenericEJB
返回 null。这是 GenericEJB 的内容
@Stateless
@LocalBean
public class GenericEJB{
@PersistenceContext(unitName = "someWebPUnit")
private EntityManager em;
public void create(Object t){
em.persist(t);
}
}
知道为什么 GenericEJB
的注入返回 null 吗?
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 injection 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建这样的对象时:
EJB 容器对此一无所知,容器不管理该对象的生命周期,它也无法注入任何内容、将其包装在方面、应用事务行为。简单地说:对于您的应用程序服务器来说,这个对象不存在。
当然,如果你用@Stateless注释
PackageProcessor
并将其作为普通EJB注入,它就会工作。你有什么理由不想这样做吗?When you create an object like this:
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?