Drop the requirement for no-args constructors in @ConfigProperties
beans
#760
Labels
use case 💡
An issue which illustrates a desired use case for the specification
Milestone
Description
As a:
...I need to be able to:
Aggregate config properties in beans with non-trivial constructors in particular record classes.
More generally, I want to remove all restriction
@ConfigProperties
beans. They should just be (almost) normal class beans and support every CDI feature like interception & decoration, non-trivial bean constructors,PostConstruct
&@PreDestroy
methods...Currently, the smallrye implementation imposes some (unspecified) restrictions, e.g.
@ConfigProperties
beans cannot have@PostConstruct
methods or producer methods, because smallrye vetoes the original and adds a synthetic bean which loses everything but the information about field injection points....which enables me to:
a.) write more concise code with records, because it is no longer necessary to have injection point fields and write getter for all of them. Everything could in principle be done in the canonical constructor of a record if only I were able to write (taking the example from the spec)
This can work for records, but not other classes, because the names of the parameters of the canonical constructor are always retained in the class file as the names of the record components. So the problem that constructor- and method-parameter names need not be present for reflection, does not appear with records.
Records also have the nice benefit that they provide a canonical place to do validation of the aggregated properties.
b.) do non-trivial validation in
@PostConstruct
, e.g.or again stealing from the example in the spec:
Some obvious validations like this one can be done with the smallrye implementation by using Jakarta Bean Validation's
@Min
annotation, but for something like theRange
example that needs to correlate multiple config properties, a single annotation is not sufficient, but writing a custom Validator seems like overkill. Also it's certainly a non-trivial leap for a developer to use Bean Validation.c.) support computed default values, e.g.
At the moment this would need to be done in custom getter methods for example.
d.) use producer methods directly in
@ConfigProperties
to provide simplified/transformed views of the config, e.g.At the moment, this does not work with smallrye, because of how
@ConfigProperties
is implemented. One needs a separate bean class for the producer methods.Implementation details
The requirement of a no-args constructor seems like an implementation detail to me. It took some fiddling, but I have a proof of concept that shows that this detail is not necessary and one can in fact integrate with a CDI container without relying on this restriction.
The text was updated successfully, but these errors were encountered: