如何在@Faces转换器中注入@EJB、@PersistenceContext、@Injection、@Autored等?我怎样才能注入类似的依赖项@EJB, @PersistenceContext, @Inject, @AutoWired,在.中.@FacesConverter?在我的特定情况下,我需要通过@EJB:@FacesConverterpublic class MyConverter implements Converter {
@EJB
protected MyService myService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// myService.doSomething
}}然而,它没有被注射,它仍然null,导致NPEs。看来@PersistenceContext和@Inject也不管用。如何在转换器中注入服务依赖项,以便访问DB?
3 回答
倚天杖
TA贡献1828条经验 获得超3个赞
@FacesConverter(forClass=Product.class)@Named@ApplicationScopedpublic class ProductConverter implements Converter{ @EJB protected ProductFacade facade; protected ProductFacade getFacadeFromConverter(FacesContext ctx){ if(facade==null){ facade = ((ProductConverter) ctx.getApplication() .evaluateExpressionGet(ctx,"#{productConverter}",ProductConverter.class)) .facade; } return facade; } @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { return getFacadeFromConverter(context).find(Long.parseLong(value)); }...
添加回答
举报
0/150
提交
取消