Quarkus 不以编程方式 select bean

Quarkus does not select bean programatically

我正在尝试以编程方式 select bean,但是 quarkus 没有注入 bean 并抛出异常。不支持?

public enum ReportType {
    ONE,
    TWO
}
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, PARAMETER, FIELD, TYPE})
@Documented
public @interface Report {

    ReportType value();

    public static final class Literal extends AnnotationLiteral<Report> implements Report {

        private final ReportType value;

        public static Literal of(ReportType value) {
            return new Literal(value);
        }

        private Literal(ReportType value) {
            this.value = value;
        }

        public ReportType value() {
            return value;
        }
    }
}
public interface CommonnInterface {
    void call();
}
@Report(value = ReportType.ONE)
public class ReportOneBusiness implements CommonnInterface {

    @Override
    public void call() {
        System.out.println("Hello");
    }
}

当我们调用


        CommonnInterface commonnInterface = CDI.current()
                .select(
                        CommonnInterface.class,
                        Report.Literal.of(ReportType.ONE)
                ).get();

未找到所需类型 [接口 org.business.CommonnInterface] 和限定符 [[@org.cdi.Report(value=ONE)]]

的 bean

您可能需要使用 @io.quarkus.arc.Unremovable 注释使 bean 不可移除。

有关详细信息,请参阅 this

geoand 是对的,我忘了把@Dependent 放在 ReportOneBusiness 中。

ReportOneBusiness 的正确代码是

@Unremovable
@Dependent
@Report(value = ReportType.ONE)
public class ReportOneBusiness extends CommonnInterface {

    @Override
    public void call() {
        System.out.println("Hello");
    }
}