Inject different types of the same class

1

I'm doing an exercise with @Inject of package javax.inject and I have no problem for example when in a class I think something like this

public class Persona {
    @Inject 
    private Rol rol;
}

but when I try to do the following, it shows me that it is ambiguous

public class Persona {
    @Inject 
    private Rol rol;
    @Inject 
    private List<Rol> roles;
}

I know I can use Qualifiers but the issue is that it's the same class I want to have role as a single object and roles as multiple role objects,

So far I only inject the single object and the list of roles initialize it in the normal way

List<Rol> roles = new ArrayList<>();
    
asked by jam 28.12.2016 в 01:01
source

1 answer

1

You can use a set of Any e Instance for what you need. The benefit of Instance is that it is a Iterable , therefore you can apply a for improved loop:

@Inject
@Any
private Instance<Rol> roles;

Reply adapted from: Inject list of objects in CDI (Weld)

    
answered by 28.12.2016 / 04:36
source