如何通过 glassfish 容器以编程方式填充@EJB 注释字段?
How to fill @EJB annotated fields programmatically via glassfish container?
我有以下问题。我已经定义了一个接口:
@Local
public interface ProductTypeLister {
Collection<ListElement> getList();
}
此外,我已经为这个东西定义了一个实现:
@Stateless(name = "ProductTypeLister")
@Local(ProductTypeLister.class)
public class ProductTypeListerImpl implements ProductTypeLister {
@Override
public Collection<ListElement> getList() {
// Implementation code
}
}
现在我想通过注解来使用它:
public class ListProductTypeAction extends ActionHandler {
@EJB
protected ProductTypeLister lister;
@Override
public String execute(HttpServletRequest request) throws ServletException {
request.setAttribute("list", lister.getList());
return "listPage.jsp";
}
}
但是我得到了 NullPointerException,因为没有填充 lister。现在的问题是,ActionHandler 不是 HttpServlet 的派生物,也不是由容器本身执行的。相反,我们正在使用 FrontController 模式 - 即有一个 servlet 可以处理所有传入的消息并通过简单的构造创建所需的处理程序。因此,EJB 注释字段不会自动填充。但是,我可以通过以下方式填写此特定案例:
public ListProductTypeAction() throws NamingException {
InitialContext context = new InitialContext();
lister = (ProductTypeLister) context.lookup("<PATH>/ProductTypeLister");
}
在那种情况下,一切正常,但这意味着我必须在 JNDI 帮助下为每个处理程序执行指定的查找,但我只想在注释帮助下获取它。因此,主servlet需要如下方法:
private void fillHandler(ActionHandler handler) {
// Fill @EJB annotated fields
}
但是我该如何填写呢?当然,我可以手动运行遍历每个字段,检查它是否是EJB注解,并根据接口不合格名称使用JNDI填充。但是有没有办法使用我已有的库来做到这一点?毕竟,Glassfish 应该在部署阶段填充这些字段。我怎样才能让他为非 servlet 做到这一点 class?
Dependency injection is the simplest way of obtaining an enterprise bean reference. Clients that run within a Java EE server-managed environment,
JavaServer Faces web applications, JAX-RS web services, other
enterprise beans, or Java EE application clients, support dependency
injection using the javax.ejb.EJB annotation.
Applications that run outside a Java EE server-managed environment,
such as Java SE applications, must perform an explicit lookup. JNDI
supports a global syntax for identifying Java EE components to
simplify this explicit lookup.
所以即使你的 class 在 J2EE 容器中,但它的生命周期不是由容器管理的,你运气不好,你必须手动完成。
我有以下问题。我已经定义了一个接口:
@Local
public interface ProductTypeLister {
Collection<ListElement> getList();
}
此外,我已经为这个东西定义了一个实现:
@Stateless(name = "ProductTypeLister")
@Local(ProductTypeLister.class)
public class ProductTypeListerImpl implements ProductTypeLister {
@Override
public Collection<ListElement> getList() {
// Implementation code
}
}
现在我想通过注解来使用它:
public class ListProductTypeAction extends ActionHandler {
@EJB
protected ProductTypeLister lister;
@Override
public String execute(HttpServletRequest request) throws ServletException {
request.setAttribute("list", lister.getList());
return "listPage.jsp";
}
}
但是我得到了 NullPointerException,因为没有填充 lister。现在的问题是,ActionHandler 不是 HttpServlet 的派生物,也不是由容器本身执行的。相反,我们正在使用 FrontController 模式 - 即有一个 servlet 可以处理所有传入的消息并通过简单的构造创建所需的处理程序。因此,EJB 注释字段不会自动填充。但是,我可以通过以下方式填写此特定案例:
public ListProductTypeAction() throws NamingException {
InitialContext context = new InitialContext();
lister = (ProductTypeLister) context.lookup("<PATH>/ProductTypeLister");
}
在那种情况下,一切正常,但这意味着我必须在 JNDI 帮助下为每个处理程序执行指定的查找,但我只想在注释帮助下获取它。因此,主servlet需要如下方法:
private void fillHandler(ActionHandler handler) {
// Fill @EJB annotated fields
}
但是我该如何填写呢?当然,我可以手动运行遍历每个字段,检查它是否是EJB注解,并根据接口不合格名称使用JNDI填充。但是有没有办法使用我已有的库来做到这一点?毕竟,Glassfish 应该在部署阶段填充这些字段。我怎样才能让他为非 servlet 做到这一点 class?
Dependency injection is the simplest way of obtaining an enterprise bean reference. Clients that run within a Java EE server-managed environment, JavaServer Faces web applications, JAX-RS web services, other enterprise beans, or Java EE application clients, support dependency injection using the javax.ejb.EJB annotation.
Applications that run outside a Java EE server-managed environment, such as Java SE applications, must perform an explicit lookup. JNDI supports a global syntax for identifying Java EE components to simplify this explicit lookup.
所以即使你的 class 在 J2EE 容器中,但它的生命周期不是由容器管理的,你运气不好,你必须手动完成。