java 什么是 EJB 3.0 版本的 ejbCreate 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7920981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:02:04  来源:igfitidea点击:

What is the EJB 3.0 version of method ejbCreate

javajakarta-eeejb-3.0ejb-2.x

提问by Stefan

I would like to migrate some old EJB 2.1 code to EJB 3.0, but there is some handling of configuration errors in the ejbCreate method. Is there an EJB 3 version of that method?

我想将一些旧的 EJB 2.1 代码迁移到 EJB 3.0,但是在 ejbCreate 方法中有一些配置错误的处理。是否有该方法的 EJB 3 版本?

Edit: In EJB 2.x ejbCreate could throw a CreateException. Based on the documentation of @PostConstruct etc. I can no longer throw any checked Exceptions. How can i handle this if i cannot migrate the code using the EJB right now.

编辑:在 EJB 2.x 中,ejbCreate 可能会抛出 CreateException。基于@PostConstruct 等的文档,我不能再抛出任何已检查的异常。如果我现在无法使用 EJB 迁移代码,我该如何处理。

Edit2: The frontend specifically handles CreateException which unfortunately is checked.

Edit2:前端专门处理 CreateException 不幸被检查。

回答by Tomasz Nurkiewicz

@PostConstruct
public void anyName() {
    //initialization code, dependencies are already injected
}

No only the name is arbitrary, you can have several @PostConstructmethods in one EJB - however the order of invocation is unspecified, so be careful and try to stick with one method.UPDATE:

不仅名称是任意的,您还可以@PostConstruct在一个 EJB 中拥有多个方法——但是调用顺序是未指定的,所以要小心并尽量坚持使用一种方法。更新:

Only one method can be annotated with this annotation.

这个注解只能注解一种方法。

回答by Kal

You need to use EJB 3.0 lifecycle callback methods using annotations

您需要使用带有注解的 EJB 3.0 生命周期回调方法

@PostConstruct, @PreDestroy, @PostActivate or @PrePassivate

These annotations can go on any method that is public, void and no-arg.

这些注释可以用于任何公共、无效和无参数的方法。

回答by Brett Kail

If the client was explicitly handling CreateException thrown by ejbCreate and you want to use EJB 3, then you must be using a stateful session bean. Exceptions from ejbCreate from stateless session beans are not propagated to clients, and entity beans do not support annotations in EJB 3. In that case, you want the @Init annotation:

如果客户端显式处理 ejbCreate 抛出的 CreateException 并且您想使用 EJB 3,那么您必须使用有状态会话 bean。来自无状态会话 bean 的 ejbCreate 异常不会传播到客户端,并且实体 bean 不支持 EJB 3 中的注释。在这种情况下,您需要 @Init 注释:

public interface MyHome extends EJBLocalHome {
  public MyInterface create(int arg) throws CreateException;
}

@Stateful
@LocalHome(MyHome.class)
public class MyBean {
  @Init
  public void init(int arg) throws CreateException {
    if (arg < 0) {
      throw new CreateException();
    }
  }
}