问题描述
我认为这个问题提高了,因为我没有得到EJB的东西。我的实体有一个服务类,这是@Stateless。当我使用@EJB在我的会话作用域演示模型中使用它,一切都可以。但是现在我想在我的演示文稿模型中覆盖的DataModel中使用这个EJB服务:
I think this problem raises because I didn't get something with EJBs. I have a service class for my entity, which is @Stateless. When I use it by injecting with @EJB in my session scoped presentation model, all is ok. But now I wanted to use this EJB service in a DataModel I have overwritten for use in my presentation model:
public class LazyUserDataModel extends LazyDataModel<User> {
@EJB
private UserService service;
@Override
public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
List<User> users;
users= service.findAllUsers();
this.setRowCount(users.size());
return users;
}
}
执行时,我在位置users = service.findAllUsers();同样的作品,当我在演示文稿模型中覆盖这个DataModel时:
On execution I get a NullPointerException at position "users= service.findAllUsers();" The same works, when I overwrite this DataModel in my presentation model:
@Named
@SessionScoped
public class UserPM {
@EJB
private UserService service;
private LazyDataModel<User> lazyUsers;
public UserPM() {
// Don't works
//lazyUsers = new LazyUserDataModel();
lazyUsers = new LazyDataModel() {
@Override
public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
List<User> users;
users = service.findAllUsers();
this.setRowCount(users .size());
return users ;
}
};
}
}
不可能在一个普通Java类?我必须做什么,我不必在演示模型中定义DataModel?
Isn't it possible to Inject an EJB in a normal Java Class? What must I do that I don't have to define the DataModel in the presentation model?
谢谢
推荐答案
EJB只注入托管bean。一个bean由一些注入容器管理,例如通过JSF自己的 @ManagedBean
,CDI的 @Named
你甚至可以在另一个EJB中注入一个EJB。您不能在非托管类中注入EJB(但是您可以手动从JNDI抓取它,但这很简单)。
EJBs are only injected in managed beans. A bean is managed when it's managed by some injection container, such as via JSF's own @ManagedBean
, CDI's @Named
, etc. You can even inject an EJB in another EJB. You cannot inject an EJB in an unmanaged class (you can however manually grab it from JNDI, but that's plain ugly).
您基本上有以下选项: p>
You've basically the following options:
-
在您的托管bean的
@PostConstruct
中,构建您传递的数据模型结果作为参数(请注意,这也是标准数据模型,如工作)
In
@PostConstruct
of your managed bean, construct the datamodel whereby you pass the result as argument (note that this is also how standard data models likeListDataModel
work).
@PostConstruct
public void init() {
lazyUsers = new LazyUserDataModel(service.findAllUsers());
}
code> LazyUserDataModel 摘要,您可以要求用户提供结果。
Make LazyUserDataModel
abstract whereby you ask the user to provide the result.
public abstract class LazyUserDataModel extends LazyDataModel<User> {
@Override
public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
List<User> users;
users = findAllUsers();
this.setRowCount(users.size());
return users ;
}
public abstract List<User> findAllUsers();
}
,以使匿名类更少
lazyUsers = new LazyUserDataModel() {
@Override
public List<User> findAllUsers() {
return service.findAllUsers();
}
};
Make LazyUserDataModel
一个托管的bean,并注入它。
Make LazyUserDataModel
a managed bean as well and inject it instead.
@Named @RequestScoped
public class LazyUserDataModel extends LazyDataModel<User> {
// ...
}
with
@Inject
private LazyUserDataModel lazyUsers;
创建一个完整的匿名实例,你认为。
Create a fullfledged anonymous instance like as you figured.
无关具体的问题是没有一个可以提供所有记录的 LazyDataModel
。它的意图在于它提供了使用SQL权限( LIMIT
, OFFSET
仅请求子集或记录的可能性,朋友)基于目前的分页状态,以便您不需要在Java的内存中拥有数百个甚至不是数千条记录,但只有十个左右。换句话说,如果你从来没有使用第一个
和/或 pageSize
参数加载()
方法,你很有可能完全错误。
Unrelated to the concrete problem, there's no point of having a LazyDataModel
whereby you provide all records. Its intent is that it offers you the possibility to request only a subset or records using SQL powers (LIMIT
, OFFSET
and friends) based on the current paginated state so that you don't need to have hundreds if not thousands records in Java's memory but only ten or so. In other words, if you are never using first
and/or pageSize
argument of the load()
method, you're most likely approaching it completely wrong.
这篇关于在Java类中注入EJB时,NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!