[英]Better way for inject EJB bean to @Named or @ManagedBean
Which way is better for inject EJB Bean? 哪种方法对注入EJB Bean更好? I have an EJB: 我有一个EJB:
@Stateless
public class BrandModel implements BrandService {
//...
}
and I have JSF @Named
bean, where I inject my EJB: 我有JSF @Named
bean,在其中注入我的EJB:
@Named
@RequestScoped
public class BrandBean implements Serializable {
@EJB
private BrandService brandService;
//...
}
but if I create a new @Named
or @ManagedBean
, for example ClotherBean
, in which i will need to use the EJB BrandModel
, which way is better for injecting the EJB? 但是,如果我创建一个新的@Named
或@ManagedBean
,例如ClotherBean
,则需要在其中使用EJB BrandModel
,哪种方法对注入EJB更好? This: 这个:
@Named
@SessionScoped
public class ClotherBean implements Serializable {
@EJB
private BrandService brandService;
//...
}
or to create a getter in BrandBean
for brandService
and use it this way: 或在BrandBean
为brandService
创建吸气剂并以这种方式使用它:
@Named
@SessionScoped
public class ClotherBean implements Serializable {
@Inject
private BrandBean brandBean;
public void test(){
brandBean.getBrandService().selectAll();
}
}
Is there a difference between these two approaches? 这两种方法之间有区别吗? Or both are ways the same? 还是两种方法都一样?
Either way is acceptable. 两种方法都可以接受。 But since Java 6+ @Inject is preferred. 但是由于Java 6+ @Inject是首选。 The behaviour has to be the same no matter what AS you use. 无论使用哪种AS,其行为都必须相同。 Here is a little bit more about on this issue: 以下是有关此问题的更多信息:
http://germanescobar.net/2010/04/4-areas-of-possible-confusion-in-jee6.html http://germanescobar.net/2010/04/4-areas-of-possible-confusion-in-jee6.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.