-
Notifications
You must be signed in to change notification settings - Fork 118
Description
The Spec does not explicitly state what kind of CDI Bean a class annotated with @ConfigProperties should give rise to. The way I understand it, it MUST be a dependent bean. My reasons are:
- The class itself from the example is annotated with
@Dependent - The
prefixattribute in@ConfigPropertiesis@NonBinding. That means that the two example injection points
@Inject
@ConfigProperties
Details serverDetails;
and
@Inject
@ConfigProperties(prefix="client")
Details clientDetails;
are expected to resolve to the same bean. The only way the CDI spec allows for one bean to create two different instances depending on the injection point is for the bean to be @Dependent scoped.
3. Programmatic lookup with arbitrary prefixes only works if it is a @Dependent bean for the same reason.
HOWEVER: In the TCK there is a test with a @RequestScoped @ConfigProperties bean.
The only way for a normal scoped bean to give different instances for different injection points is to have the prefix attribute be binding again. And in fact, that is what smallrye does: It removes the @NonBinding annotation during BeforeBeanDiscovery and creates many, many synthetic beans from one @ConfigProperties class - one for each prefix that is discovered at injection points. And all of them are @Dependent scoped, even though the class specified @RequestScoped. This feels like cheating.
But that has another consequence: Programmatic lookup of @ConfigProperties no longer works as I would expect it. Again, the spec is not 100% clear here. The only example given is
Details details = CDI.current().select(Details.class, ConfigProperties.Literal.NO_PREFIX).get();
Does that mean that this is the only case of programmatic lookup that is required to work? Or is it required that programmatic lookup with arbitrary prefixes works? I would have expected the latter to be the case (and indeed I have filed this as a smallrye bug )
The same TCK case also contains the bean
@ConfigProperties(prefix = "cloud")
public static class BeanFour {
@ConfigProperty(name = "a.host", defaultValue = "mycloud.org")
private String host;
public int port = 9080;
public String getHost() {
return host;
}
public Optional<String> location;
}
and the way it is used in the test case suggests that the field initializer replaces the need for a default value.
Where does this requirement come from? I cannot find it in the spec. And it is ... let's say weird ... from an implementation stand point. I tried to find the place where this is implementation in smallrye and could not find it. Maybe it is very well hidden, maybe smallrye just accidentally satisfies this testcase?