RepositoryConfigurationDelegate#registerRepositoriesIn
public class RepositoryConfigurationDelegate { public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegistry registry, RepositoryConfigurationExtension extension) { extension.registerBeansForRoot(registry, this.configurationSource); RepositoryBeanDefinitionBuilder builder = new RepositoryBeanDefinitionBuilder(registry, extension, this.configurationSource, this.resourceLoader, this.environment); List<BeanComponentDefinition> definitions = new ArrayList(); StopWatch watch = new StopWatch(); watch.start(); //extension.getRepositoryConfigurations() 会扫描相应的包并找到用户自定义JpaRepository接口 Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configurations = extension.getRepositoryConfigurations(this.configurationSource, this.resourceLoader, this.inMultiStoreMode); Map<String, RepositoryConfiguration<?>> configurationsByRepositoryName = new HashMap(configurations.size()); Iterator var8 = configurations.iterator(); while(var8.hasNext()) { RepositoryConfiguration<? extends RepositoryConfigurationSource> configuration = (RepositoryConfiguration)var8.next(); configurationsByRepositoryName.put(configuration.getRepositoryInterface(), configuration); //对于每个扫描找到的用户自定义JpaRepository,构建一个BeanDefinitionBuilder, //就是在这个步骤中将该BeanDefinition同JpaRepositoryFactoryBean建立关系 BeanDefinitionBuilder definitionBuilder = builder.build(configuration); extension.postProcess(definitionBuilder, this.configurationSource); if (this.isXml) { extension.postProcess(definitionBuilder, (XmlRepositoryConfigurationSource)this.configurationSource); } else { extension.postProcess(definitionBuilder, (AnnotationRepositoryConfigurationSource)this.configurationSource); } //这里根据所发现的用户自定义JpaRepository接口的名字构造一个bean名称 AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition(); beanDefinition.setResourceDescription(configuration.getResourceDescription()); String beanName = this.configurationSource.generateBeanName(beanDefinition); if (logger.isTraceEnabled()) { logger.trace(LogMessage.format("Spring Data %s - Registering repository: %s - Interface: %s - Factory: %s", extension.getModuleName(), beanName, configuration.getRepositoryInterface(), configuration.getRepositoryFactoryBeanClassName())); } //设置当前BeanDefinition的属性factoryBeanObjectType为用户自定义JpaRepository接口的全限定名 beanDefinition.setAttribute("factoryBeanObjectType", configuration.getRepositoryInterface()); // 现在把这个bean注册到容器 registry.registerBeanDefinition(beanName, beanDefinition); definitions.add(new BeanComponentDefinition(beanDefinition, beanName)); } potentiallyLazifyRepositories(configurationsByRepositoryName, registry, this.configurationSource.getBootstrapMode()); watch.stop(); return definitions; } }RepositoryBeanDefinitionBuilder#build
public BeanDefinitionBuilder build(RepositoryConfiguration<?> configuration) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(configuration.getRepositoryFactoryBeanClassName()); builder.getRawBeanDefinition().setSource(configuration.getSource()); builder.addConstructorArgValue(configuration.getRepositoryInterface()); builder.addPropertyValue("queryLookupStrategyKey", configuration.getQueryLookupStrategyKey()); builder.addPropertyValue("lazyInit", configuration.isLazyInit()); builder.setLazyInit(configuration.isLazyInit()); builder.setPrimary(configuration.isPrimary()); configuration.getRepositoryBaseClassName().ifPresent((it) -> { builder.addPropertyValue("repositoryBaseClass", it); }); NamedQueriesBeanDefinitionBuilder definitionBuilder = new NamedQueriesBeanDefinitionBuilder(this.extension.getDefaultNamedQueryLocation()); configuration.getNamedQueriesLocation().ifPresent(definitionBuilder::setLocations); builder.addPropertyValue("namedQueries", definitionBuilder.build(configuration.getSource())); this.registerCustomImplementation(configuration).ifPresent((it) -> { builder.addPropertyReference("customImplementation", it); builder.addDependsOn(it); }); BeanDefinitionBuilder fragmentsBuilder = BeanDefinitionBuilder.rootBeanDefinition(RepositoryFragmentsFactoryBean.class); List<String> fragmentBeanNames = (List)this.registerRepositoryFragmentsImplementation(configuration).map(RepositoryFragmentConfiguration::getFragmentBeanName).collect(Collectors.toList()); fragmentsBuilder.addConstructorArgValue(fragmentBeanNames); builder.addPropertyValue("repositoryFragments", ParsingUtils.getSourceBeanDefinition(fragmentsBuilder, configuration.getSource())); return builder; }