Java 类org.springframework.data.repository.core.support.AbstractRepositoryMetadata 实例源码

项目:spring-data-mybatis    文件:MybatisRepositoryConfigExtension.java   
@Override
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource config) {
    super.registerBeansForRoot(registry, config);


    Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
    Collection<BeanDefinition> candidates = config.getCandidates(resourceLoader);
    for (BeanDefinition candidate : candidates) {
        Class<?> repositoryInterface = loadRepositoryInterface(candidate.getBeanClassName(), resourceLoader);
        if (null == repositoryInterface) {
            continue;
        }

        RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(repositoryInterface);
        if (null == metadata) {
            continue;
        }

        initialEntitySet.add(metadata.getDomainType());
    }


    Object source = config.getSource();
    String sqlSessionFactoryRef = config.getAttribute("sqlSessionFactoryRef");
    sqlSessionFactoryRef = (null == sqlSessionFactoryRef ? DEFAULT_SQL_SESSION_FACTORY_BEAN_NAME : sqlSessionFactoryRef);


    BeanDefinitionBuilder mappingContextBuilder = BeanDefinitionBuilder.rootBeanDefinition(MybatisMappingContext.class);
    mappingContextBuilder.addPropertyValue("initialEntitySet", initialEntitySet);
    registerIfNotAlreadyRegistered(mappingContextBuilder.getBeanDefinition(), registry, sqlSessionFactoryRef.concat(MAPPING_CONTEXT_SUFFIX), source);


    // create database dialect
    BeanDefinitionBuilder dialectBuilder = BeanDefinitionBuilder.rootBeanDefinition(DialectFactoryBean.class);
    dialectBuilder.addPropertyReference("sqlSessionFactory", sqlSessionFactoryRef);
    registerIfNotAlreadyRegistered(dialectBuilder.getBeanDefinition(), registry, sqlSessionFactoryRef.concat(DIALECT_BEAN_NAME_SUFFIX), source);


    // create sqlSessionTemplate bean.
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(SqlSessionTemplate.class);
    builder.addConstructorArgReference(sqlSessionFactoryRef);
    registerIfNotAlreadyRegistered(builder.getBeanDefinition(), registry, sqlSessionFactoryRef.concat(SQL_SESSION_TEMPLATE_BEAN_NAME_SUFFIX), source);

    // create mybatis mapper register
    if (config instanceof MybatisAnnotationRepositoryConfigurationSource) {
        String[] mapperLocations = ((MybatisAnnotationRepositoryConfigurationSource) config).getMapperLocations();
        if (null != mapperLocations && mapperLocations.length > 0) {
            BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.rootBeanDefinition(MybatisMappersRegister.class);
            builder1.addPropertyReference("sqlSessionFactory", sqlSessionFactoryRef);
            builder1.addPropertyValue("locations", mapperLocations);
            registerIfNotAlreadyRegistered(builder1.getBeanDefinition(), registry, sqlSessionFactoryRef.concat("_MapperLocations"), source);
        }
    }

}