/*
   * (non-Javadoc)
   * @see org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser#postProcessBeanDefinition(org.springframework.data.repository.config.SingleRepositoryConfigInformation, org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object)
   */
  @Override
  protected void postProcessBeanDefinition(
      MongoRepositoryConfiguration context,
      BeanDefinitionBuilder builder,
      BeanDefinitionRegistry registry,
      Object beanSource) {

    builder.addPropertyReference("template", context.getMongoTemplateRef());

    String mappingContextRef = getMappingContextReference(context, registry);
    if (mappingContextRef != null) {
      builder.addPropertyReference("mappingContext", mappingContextRef);
    }
  }
  /**
   * Returns the bean name of a {@link MappingContext} to be wired. Will inspect the namespace
   * attribute first and if no config is found in that place it will try to lookup the default one.
   * Will return {@literal null} if neither one is available.
   *
   * @param config
   * @param registry
   * @return
   */
  private String getMappingContextReference(
      MongoRepositoryConfiguration config, BeanDefinitionRegistry registry) {

    String contextRef = config.getMappingContextRef();

    if (contextRef != null) {
      return contextRef;
    }

    try {
      registry.getBeanDefinition(MAPPING_CONTEXT_DEFAULT);
      return MAPPING_CONTEXT_DEFAULT;
    } catch (NoSuchBeanDefinitionException e) {
      return null;
    }
  }