简体   繁体   中英

Better way for inject EJB bean to @Named or @ManagedBean

Which way is better for inject EJB Bean? I have an EJB:

@Stateless
public class BrandModel implements BrandService {
//...
}

and I have JSF @Named bean, where I inject my 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? 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:

@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. The behaviour has to be the same no matter what AS you use. Here is a little bit more about on this issue:

http://germanescobar.net/2010/04/4-areas-of-possible-confusion-in-jee6.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM