托管 Bean EJB 注入
我有很多托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您执行以下操作,因为显然您有很多服务并且希望将它们组合在一起,您可以创建一个“抽象”bean,并使您的托管 bean 扩展这样的“< em>abstract”bean,这样你就可以以结构化和安全的方式访问EJB,下面的代码将解释我的意思:
请参考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:
Please refer to JavaEE5 EJB FAQ if you need to clarify more concepts on the matter.