【发布时间】:2015-04-16 17:45:26
【问题描述】:
我是 EJB 和 CDI 的新手。 请帮助我理解我在这里做错了什么:
我的代码如下,并部署在 JBoss WildFly 8 上的 WAR 中:
@Stateless(name = "application.listDao")
public class ListDao extends BaseDao {
@Inject
private SomeOtherDao someOtherDao;
// some other methods
}
@Stateless
public abstract class BaseDao {
@Inject
protected EntityManager entityManager;
public List find( long id ) {
List list = new ArrayList<>();
// JPA stuff to perform operations
return list;
}
}
现在,我将这个 ListDao 注入到部署在同一个 Wildfly 实例上的其他 WAR 中:
@RequestScoped
public class ListReport {
@Inject
private ListDao listDao;
public List getReport(long id) {
// Here I am getting NullPointerException
List reportList = listDao.find(id);
return reportList;
}
}
我将 listDao 设为 null,因此得到 NullPointerException。 通过在 WEB-INF 文件夹下放置空 beans.xml 来启用 CDI。
【问题讨论】:
-
尝试注入接口类型而不是类类型。看这里stackoverflow.com/questions/16056881/…
-
使用
@PersistenceContext注入EntityManager而不是@Inject。 -
战争是在同一只耳朵里吗?
-
另外,我认为没有理由让您的 DAO EJB 管理。去掉
@Stateless注解,或许能解决你的问题。
标签: java jakarta-ee ejb cdi wildfly