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

Move login-ui Service classes to Javaconfig #3208

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

Kehrlann
Copy link
Contributor

@Kehrlann Kehrlann commented Dec 20, 2024

Context

We are moving out of XML config and into java config.

This moves *Service used by login-ui out of XML config, and can be used as a blueprint to move some config out of XML.

Below are a few notable changes to the configuration.

Configuration properties

Example: SmtpConfigurationProperties.

Avoid @Value, prefer @ConfigurationProperties classes which allow for easily following where they are actually used. Default values can be provided. Validation can be applied. Avoid SpEL expressions where possible, prefer Java code, either in constructors or in bean methodes.

For configuration properties, when possible, prefer immutable records.

Code-independent beans

Examples: EmailChangeEmailService, EmailAccountCreationService, etc.

Any bean that:

  1. Has a unique implementation
  2. Does not have conditions to decide whether it should be present or not
  3. Does not have configuration variants ("use this or that param, based on some condition")

can be provided with a stereotype annotation, such as @Component or @Service. When they are not used anywhere in XML config, they do not need to be named.

Conditional and code-dependent beans

Examples: JavaMailSender, MessageService.

Some beans are configuration-dependent. They require some code to be correctly created, and may be conditional on some application properties ("only create a NotificationsService when there is a notifications.url property"). They are typically interfaces, that have multiple implementations.

Those beans need to be provided in @Bean functions in @Configuration classes.

Typically, one implementation is the default, and other is what happens when certain conditions are met. A way to achieve this in javaconfig is to annotation the configuration-dependent bean with @Primary, and it will override the default bean. Spring Framework 6.2 / Boot 3.4 will provide an @Fallback annotation to achieve the same result.

Keep the code minimum in @Bean methods when possible, and rely on declarative configuration instead.

Reference data

With beans moved out of XML, reference data, such as util:map id="notifications", needs to be moved to java (in this case, in a helper static method, notifications()).

In this particular case we chose functions to avoid a nested Map<String, Map<String, String>>: it makes in XML or other text-based data representation, but less in Java.

@Kehrlann Kehrlann force-pushed the dgarnier/javaconfig branch from 837590c to b40c588 Compare December 20, 2024 13:59
@Kehrlann Kehrlann marked this pull request as draft December 20, 2024 14:00
@Kehrlann Kehrlann force-pushed the dgarnier/javaconfig branch from b40c588 to 765793d Compare December 20, 2024 16:34
@Kehrlann Kehrlann marked this pull request as ready for review December 20, 2024 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

1 participant