流程
- MapperScan中 @Import(MapperScannerRegistrar.class) 导入了MapperScannerRegistrar
MapperScannerRegistrar实现了ImportBeanDefinitionRegistrar 方法registerBeanDefinitions - ConfigurationClassPostProcessor实现BeanDefinitionRegistryPostProcessor spring中内置处理器internalConfigurationAnnotationProcessor
- AbstractApplicationContext中refresh()方法调用invokeBeanFactoryPostProcessors(beanFactory);调用PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());调用invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
调用ConfigurationClassPostProcessor中postProcessBeanDefinitionRegistry方法 - ConfigurationClassPostProcessor中processConfigBeanDefinitions(registry);中loadBeanDefinitions
- ConfigurationClassBeanDefinitionReader中loadBeanDefinitionsForConfigurationClass中loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());中registrars.forEach((registrar, metadata) ->
registrar.registerBeanDefinitions(metadata, this.registry));
- Driver与DataSource
- DataSource需要配置文件,xml或一Configuration(ibatis的)实例
- SqlSessionFactoryBuilder 从上述配置获取SqlSessionFactory的实例
- 不用配置文件mapper接口和mapper xml文件放同一目录
- 由xml文件找mapper接口 或者由mapper接口找xml,两者的namespace要统一?
MapperAnnotationBuilder.loadXmlResource() line: 172
MapperAnnotationBuilder.parse() line: 124
MapperRegistry.addMapper(Class<T>) line: 72
Configuration.addMappers(String) line: 737
//type是Class<?>类型
private void loadXmlResource() {
// Spring may not know the real resource name so we check a flag
// to prevent loading again a resource twice
// this flag is set at XMLMapperBuilder#bindMapperForNamespace
if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
String xmlResource = type.getName().replace('.', '/') + ".xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
} catch (IOException e) {
// ignore, resource is not required
}
if (inputStream != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
xmlParser.parse();
}
}
}
- MyBatis以SqlSessionFactory的实例为中心,SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。MyBatis以SqlSessionFactory的实例为中心,SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先定制的 Configuration 的实例构建出 SqlSessionFactory 的实例。