在运行时

时间:2017-11-14 17:36:15

标签: java spring cdi

我无法在运行时注入限定符列表。 我创建了界面并实现了它。

public interface AndamentoService {
    void realizar();
}

并实施:

@Service
@FaseAndamento(FaseAndamento.Fase.ANALISE)
public class AndamentoAnaliseServiceImpl implements AndamentoService {
    @Override
    public void realizar() {

    }
}
@FaseAndamento(FaseAndamento.Fase.MARCACAO)
@Service
public class AndamentoMarcacaoServiceImpl implements AndamentoService {
    @Override
    public void realizar() {

    }
}

我创建了限定符注释,以便能够注入我的类:

@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR})
@Qualifier
public @interface FaseAndamento {

    Fase value();

    public static enum Fase {
        ANALISE,
        MARCACAO;
    }
}

这是能够在运行时检索实现的类,就好像它是一个工厂

public class AndamentoAnnotationLiteral extends AnnotationLiteral<FaseAndamento> implements FaseAndamento{
    private Fase value;
    private AndamentoAnnotationLiteral(Fase value) {
        this.value = value;
    }
    @Override
    public Fase value() {
        return value;
    }
    public static AndamentoAnnotationLiteral andamento(Fase value){
        return new AndamentoAnnotationLiteral(value);
    }
}

当我注入我的Andamento名单时,就像这样:

@ViewScoped
@Named
@Controller
public class RealizarAndamentoController implements Serializable{

    @Inject @Any
    private Instance<AndamentoService> andamentos;

    @Inject
    @FaseAndamento(FaseAndamento.Fase.MARCACAO)
    private AndamentoService andamento;

    @PostConstruct
    public void realizarAndamento(){
        AndamentoService andamento = andamentos.select(AndamentoAnnotationLiteral.andamento(FaseAndamento.Fase.ANALISE)).get();
        andamento.realizar();
    }
}

我收到以下错误: NoSuchBeanDefinitionException:没有类型为javax.enterprise.inject.Instance service.AndamentoService的限定bean

0 个答案:

没有答案
相关问题