Leandro Jordao
Leandro Jordao

Reputation: 23

Quarkus does not select bean programatically

I'm trying to select the bean programatically, but quarkus does not injected the bean and throw an exception. It's not supported ?

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");
    }
}

And when we call


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

No bean found for required type [interface org.business.CommonnInterface] and qualifiers [[@org.cdi.Report(value=ONE)]]

Upvotes: 2

Views: 1369

Answers (2)

Leandro Jordao
Leandro Jordao

Reputation: 23

geoand was right, and I forgot to put @Dependent in the ReportOneBusiness.

The right code for ReportOneBusiness is

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

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

Upvotes: 0

geoand
geoand

Reputation: 64011

You likely need to make the beans unremoveable using the @io.quarkus.arc.Unremovable annotation.

See this for more details.

Upvotes: 5

Related Questions