I have the following class:
@Configuration
@Component
@Getter
public class EndUserDisambiguator extends UserDisambiguator {
private final Collection<UserCredentialDisambiguator<?, ?, ?>> endUserCredentialDisambiguators;
@Autowired
public EndUserDisambiguator(final EndUserRepository userRepository) {
super(userRepository);
val phoneNumberDisambiguator = new UserCredentialDisambiguator<>(EndUser::getPhoneNumber, userRepository::findByPhoneNumber, PhoneNumberExistsException::new);
val socialSecurityNumberDisambiguator = new UserCredentialDisambiguator<>(EndUser::getSocialSecurityNumber, userRepository::findBySocialSecurityNumber, SocialSecurityNumberExistsException::new);
this.endUserCredentialDisambiguators = Set.of(phoneNumberDisambiguator, socialSecurityNumberDisambiguator);
}
@Override
public Collection<UserCredentialDisambiguator<?, ?, ?>> getUserCredentialDisambiguators() {
val baseCredentialDismabiguators = super.getUserCredentialDisambiguators().stream();
val specificUserCredentialDisambiguators = getEndUserCredentialDisambiguators().stream();
val userCredentialDisambiguators = Stream.concat(baseCredentialDismabiguators, specificUserCredentialDisambiguators);
return userCredentialDisambiguators.collect(Collectors.toUnmodifiableSet());
}
@Bean(UserDisambiguator.END_USER_QUALIFIER)
public EndUserDisambiguator getEndUserDisambiguator() {
return this;
}
}
This is part of the disambiguation layer of my application, the layer that ensures any unpersisted users created in the service layer do not interfere with existing persisted users (for example, ensuring usernames are unique). `
It therefore is @Autowired
a reference to a user repository, and only the disambiguation layer is @Autowired
a repository (the service layer is not). The service layer does its persisting through the disambiguation layer.
EndUserService
(the corresponding service for the same type of user) therefore requires an @Autowired
reference to EndUserDisambiguator
. I do not want to leak the repository layer to service layer (the service layer does its persisting post-disambiguation). Is it a design flaw to declare EndUserDisambiguator
a @Configuration
and have a method returning a @Bean
corresponding to itself?
Here is where EndUserDisambiguator
is injected into:
@Service
public class EndUserService extends UserService {
@Autowired
public EndUserService(final VerificationTokenRepository verificationTokenRepository, final EndUserDisambiguator userDisambiguator) {
super(verificationTokenRepository, userDisambiguator);
}
...
}