-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add another example for a constraint validator with an injected bean #46130
base: main
Are you sure you want to change the base?
Add another example for a constraint validator with an injected bean #46130
Conversation
Status for workflow
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I added a few comments.
@@ -411,6 +411,44 @@ The scope you choose for your `ConstraintValidator` bean is important: | |||
- If the `ConstraintValidator` bean implements the `initialize(A constraintAnnotation)` method and depends on the state of the constraint annotation, use the `@Dependent` scope to make sure each annotation context has a separate and properly configured instance of the `ConstraintValidator` bean. | |||
==== | |||
|
|||
When injecting beans relying on the runtime configuration, use `@Inject Instance<..>`. Since constraints are initialised |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use US English given the method is called initialize()
:
When injecting beans relying on the runtime configuration, use `@Inject Instance<..>`. Since constraints are initialised | |
When injecting beans relying on the runtime configuration, use `@Inject Instance<..>`. Since constraints are initialized |
at build time, it is not possible to completely preconfigure the constraint in the `initialize(..)` method, | ||
as the runtime information will be missing at that point. The `initialize(..)` method in such scenarios can still be used | ||
to read the constraint annotation parameters, and perform any work not relying on the runtime configuration. | ||
Hence, it is recommended that any heavy configuration work happens as part of the injected bean initialisation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence, it is recommended that any heavy configuration work happens as part of the injected bean initialisation | |
Hence, it is recommended that any heavy configuration work happens as part of the injected bean initialization |
public class MyService { | ||
|
||
public MyService(..) { | ||
// perform all possible "initialisation" work |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// perform all possible "initialisation" work | |
// perform all possible "initialization" work |
public class MyService { | ||
|
||
public MyService(..) { | ||
// perform all possible "initialisation" work |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're talking about runtime configuration but then we don't see it in the example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey 😃 👋🏻
thanks for taking a look and for the suggestions!
Yeah ... I didn't want to make this example long and with some code that is, most likely, irrelevant to the user, so I thought I could get away with just a comment 🙈 😃.
would the following work here:
@ApplicationScoped
public class MyService {
private final Predicate<String> validationFunction;
@Inject
public MyService(MyRuntimeConfigProperties config) {
// perform all possible "initialization" work, e.g.:
if (config.complexValidationEnabled()) {
validationFunction = s -> ...
} else {
validationFunction = String::isBlank;
}
}
public boolean validate(String value) {
// perform the validation
return validationFunction.test(value);
}
}
Status for workflow
|
🎊 PR Preview 2de91f4 has been successfully built and deployed to https://quarkus-pr-main-46130-preview.surge.sh/version/main/guides/
|
adds an example to the docs mentioned in the discussion of #43450