// Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear();
// if specified, use the given annotation and / or marker interface //只接受制定的annotation接口类 if (this.annotationClass != null) { addIncludeFilter(new AnnotationTypeFilter(this.annotationClass)); acceptAllInterfaces = false; }
// override AssignableTypeFilter to ignore matches on the actual marker interface //只接受指定类 if (this.markerInterface != null) { addIncludeFilter(new AssignableTypeFilter(this.markerInterface) { @Override //重写判断方法 protectedbooleanmatchClassName(String className){ returnfalse; } }); acceptAllInterfaces = false; }
if (acceptAllInterfaces) { // default include filter that accepts all classes addIncludeFilter(new TypeFilter() { publicbooleanmatch(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException { returntrue; } }); }
if (beanDefinitions.isEmpty()) { logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration."); } else { for (BeanDefinitionHolder holder : beanDefinitions) { //循环完善BeanDefinitionHolder的各类信息 GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
if (logger.isDebugEnabled()) { logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); }
// the mapper interface is the original class of the bean // but, the actual class of the bean is MapperFactoryBean //补充信息:mapperInterface=getBeanClassName,也就是mapper的接口类名 definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName()); //重点,英文解释很清楚了,MapperFactoryBean作为实际bean的提供方, //MapperFactoryBean实现了FactoryBean definition.setBeanClass(MapperFactoryBean作为实际bean的提供方,.class);
if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) { if (explicitFactoryUsed) { logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName)); explicitFactoryUsed = true; } elseif (this.sqlSessionTemplate != null) { if (explicitFactoryUsed) { logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate); explicitFactoryUsed = true; }
if (!explicitFactoryUsed) { if (logger.isDebugEnabled()) { logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'."); } definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); } } }
/** * Sets the mapper interface of the MyBatis mapper * * @param mapperInterface class of the interface */ publicvoidsetMapperInterface(Class<T> mapperInterface){ this.mapperInterface = mapperInterface; }
/** * If addToConfig is false the mapper will not be added to MyBatis. This means * it must have been included in mybatis-config.xml. * <p> * If it is true, the mapper will be added to MyBatis in the case it is not already * registered. * <p> * By default addToCofig is true. * * @param addToConfig */ publicvoidsetAddToConfig(boolean addToConfig){ this.addToConfig = addToConfig; }
public <T> T getMapper(Class<T> type, SqlSession sqlSession){ final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type); if (mapperProxyFactory == null) { thrownew BindingException("Type " + type + " is not known to the MapperRegistry."); } try { return mapperProxyFactory.newInstance(sqlSession); } catch (Exception e) { thrownew BindingException("Error getting mapper instance. Cause: " + e, e); } }
----------------------------- public T newInstance(SqlSession sqlSession){ final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); } ------------- //org.apache.ibatis.binding.MapperProxyFactory.newInstance(MapperProxy<T>) //利用jdk代理返回了代理类,加强(实现类为)mapperProxy protected T newInstance(MapperProxy<T> mapperProxy){ return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); }