javacdijboss-weld

how to inject a primitive using WELD


I am following this tutorial on how to use Jakarta CDI: https://jakarta.ee/learn/docs/jakartaee-tutorial/current/cdi/cdi-basic/cdi-basic.html#_injecting_objects_by_using_producer_methods

The problem is, I cannot inject a primitive (an int) due to The injection point has non-proxyable dependencies exception.

The code I'm using is as follows:

(custom qualifier)

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface MaxNumber {}

(a producer)

public class NumbersProducer {
    private int maxNumber = 100;

    @Produces @MaxNumber @RequestScoped public int getMaxNumber() {
        return maxNumber;
    }
}

(an injection point)

public class ProducersPrinter {
   @Inject @MaxNumber int maxNumber;
   
}

Is that even possible to inject a primitive using WELD?


Solution

  • The solution is remove @RequestScoped annotation from producer method.

    There is a hint in CDI specification on "restrictions upon bean types of beans with normal scopes". Normal scopes according to the specs consist of "all scopes defined by this specification, except for the @Dependent pseudo-scope".