Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marko-bekhta
Copy link
Contributor

adds an example to the docs mentioned in the discussion of #43450

Copy link

quarkus-bot bot commented Feb 6, 2025

Status for workflow Quarkus CI

This is the status report for running Quarkus CI on commit c193fdf.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

Warning

There are other workflow runs running, you probably need to wait for their status before merging.

You can consult the Develocity build scans.

Copy link
Member

@gsmet gsmet left a 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
Copy link
Member

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():

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// perform all possible "initialisation" work
// perform all possible "initialization" work

public class MyService {

public MyService(..) {
// perform all possible "initialisation" work
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link

quarkus-bot bot commented Feb 6, 2025

Status for workflow Quarkus Documentation CI

This is the status report for running Quarkus Documentation CI on commit c193fdf.

✅ The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

Copy link

github-actions bot commented Feb 6, 2025

🎊 PR Preview 2de91f4 has been successfully built and deployed to https://quarkus-pr-main-46130-preview.surge.sh/version/main/guides/

  • Images of blog posts older than 3 months are not available.
  • Newsletters older than 3 months are not available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants