托管 Bean EJB 注入

发布于 2024-11-05 05:19:29 字数 444 浏览 8 评论 0原文

我有很多托管 bean,想知道是否可以创建一个 UtilClass,其中我 把我的服务调用(@EJB)。我已经尝试过了,但遇到了 NullPointerException。 这就是我的 UtilClass 和托管 bean 的样子:

public class UtilClass{
@EJB
private static MyFirstEjbLocal myFirstService;
@EJB
private static  MySecondEjbLocal mySecondService;
//other services
//getters

 }


public class MyManagedBean{
   public String myMethod(){

   UtilClass.getMyFirstService.doSomethingInDB();

   return null;
  }
}

i'm having many managed beans and was wondering if i could create a UtilClass where i
put my services calls (@EJB). I've already tried it but i'm having a NullPointerException.
this is how my UtilClass and my managed bean look like:

public class UtilClass{
@EJB
private static MyFirstEjbLocal myFirstService;
@EJB
private static  MySecondEjbLocal mySecondService;
//other services
//getters

 }


public class MyManagedBean{
   public String myMethod(){

   UtilClass.getMyFirstService.doSomethingInDB();

   return null;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-11-12 05:19:30

我建议您执行以下操作,因为显然您有很多服务并且希望将它们组合在一起,您可以创建一个“抽象”bean,并使您的托管 bean 扩展这样的“< em>abstract”bean,这样你就可以以结构化和安全的方式访问EJB,下面的代码将解释我的意思:

public class MyAbstractBean{
  @EJB
  protected MyFirstEjbLocal myFirstService;
  @EJB
  protected  MySecondEjbLocal mySecondService;
  // All your other EJB's here
  ...
  // All other variables and methods you could need
}


public class MyManagedBean
   extends MyAbstractBean{

    public String myMethod1(){

      myFirstService.doSomethingInDB();
      return "";

    }

    public String myMethod2(){

      mySecondService.doSomethingInDB();
      return "";

    }
}

请参考JavaEE5 EJB FAQ 如果您需要澄清有关此事的更多概念。

I would suggest you to do the following, since apparently you are having a lot of services and want to have them grouped together, you can create an "abstract" bean and make your managed bean extend such "abstract" bean, in this way you can access the EJB's in a structural and safe way, the following code will explain what I mean:

public class MyAbstractBean{
  @EJB
  protected MyFirstEjbLocal myFirstService;
  @EJB
  protected  MySecondEjbLocal mySecondService;
  // All your other EJB's here
  ...
  // All other variables and methods you could need
}


public class MyManagedBean
   extends MyAbstractBean{

    public String myMethod1(){

      myFirstService.doSomethingInDB();
      return "";

    }

    public String myMethod2(){

      mySecondService.doSomethingInDB();
      return "";

    }
}

Please refer to JavaEE5 EJB FAQ if you need to clarify more concepts on the matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文