Can I @Inject a generic type?

2.1k Views Asked by At

This does not seem to work:

@Inject
private MyBBean<OtherBBean> myBean;

Should it work or is it just wrong to try to do this? I get this error:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [MyBBean<OtherBBean>] with qualifiers [@Default] at injection point [[field] @Inject private com.my.project.beans.jsf.BreakBBean.myBBean]

EDIT: Adding more info:

@Named(value="org")
@SessionScoped
public class OrgBBean extends MainViewBase<Org> implements Serializable {

    private static final long serialVersionUID = 1L;
    @Inject
    private OtherBBean otherBBean;
}


@Named
@Dependent
public class OtherBBean extends OrgTabMemberBBean<Other> implements Serializable {

    private static final long serialVersionUID = 1L;
    @Inject
    private MyBBean<OtherBBean> myBBean;
}


@Named
@Dependent
public class MyBBean <O extends EditableBase<? extends BaseEntity>> extends EditableTabListBBean<My, O> implements Serializable {

    private static final long serialVersionUID = 1L;
}
2

There are 2 best solutions below

0
On BEST ANSWER

I worked around this by just using the raw type instead. It works but now I have to be careful how I used it.

@Inject
private MyBBean myBBean;
0
On

Tested here and worked fine. Didn't you miss something, like forgetting to add @Named in the MyBBean class, or maybe using it in a @SessionScoped bean and the MyBBean doesn't implement Serializable?

EDIT: Can you post your MyBBean code?