1

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

    ...
}
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
  • how do you resolve a conflict between two beans (the `@Bean` method and the `@Component` class are of the same type) – Andrew Tobilko Aug 14 '19 at 07:29
  • Could you show 2 places where you inject `UserDisambiguator`? – Andrew Tobilko Aug 14 '19 at 07:30
  • @AndrewTobilko ah, that was a typo on my end, I've changed the bean return type to the implementation `EndUserDisambiguator` from the abstraction `UserDisambiguator` – Mario Ishac Aug 14 '19 at 07:35
  • @AndrewTobilko I don't inject `UserAmbiguator` directly, that's the abstraction. However, I inject `EndUserDisambiguator` into and only into `EndUserService`, editing that into the question – Mario Ishac Aug 14 '19 at 07:37

0 Answers0