Search in sources :

Example 1 with ClassPathMapperScanner

use of org.mybatis.spring.mapper.ClassPathMapperScanner in project cloud-sea-towerman by huadahuang1983.

the class MapperScannerRegistrar method registerBeanDefinitions.

/**
 * {@inheritDoc}
 */
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    // this check is needed in Spring 3.1
    if (resourceLoader != null) {
        scanner.setResourceLoader(resourceLoader);
    }
    Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");
    if (!Annotation.class.equals(annotationClass)) {
        scanner.setAnnotationClass(annotationClass);
    }
    Class<?> markerInterface = annoAttrs.getClass("markerInterface");
    if (!Class.class.equals(markerInterface)) {
        scanner.setMarkerInterface(markerInterface);
    }
    Class<? extends BeanNameGenerator> generatorClass = annoAttrs.getClass("nameGenerator");
    if (!BeanNameGenerator.class.equals(generatorClass)) {
        scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass));
    }
    @SuppressWarnings("rawtypes") Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean");
    if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
        scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass));
    }
    scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef"));
    scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef"));
    List<String> basePackages = new ArrayList<String>();
    for (String pkg : annoAttrs.getStringArray("value")) {
        if (StringUtils.hasText(pkg)) {
            basePackages.add(parsePlaceHolder(pkg));
        }
    }
    for (String pkg : annoAttrs.getStringArray("basePackages")) {
        if (StringUtils.hasText(pkg)) {
            basePackages.add(parsePlaceHolder(pkg));
        }
    }
    for (Class<?> clazz : annoAttrs.getClassArray("basePackageClasses")) {
        basePackages.add(ClassUtils.getPackageName(clazz));
    }
    scanner.registerFilters();
    scanner.doScan(StringUtils.toStringArray(basePackages));
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) MapperFactoryBean(org.mybatis.spring.mapper.MapperFactoryBean) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) ClassPathMapperScanner(org.mybatis.spring.mapper.ClassPathMapperScanner)

Example 2 with ClassPathMapperScanner

use of org.mybatis.spring.mapper.ClassPathMapperScanner in project loc-framework by lord-of-code.

the class LocMybatisAutoConfiguration method createClassPathMapperScanner.

private void createClassPathMapperScanner(BeanDefinitionRegistry registry, String prefixName) {
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
    try {
        if (this.resourceLoader != null) {
            scanner.setResourceLoader(this.resourceLoader);
        }
        List<String> packages = AutoConfigurationPackages.get(beanFactory);
        packages.forEach(pkg -> log.info("Using auto-configuration base package '{}'", pkg));
        scanner.setAnnotationClass(Mapper.class);
        scanner.setSqlSessionFactoryBeanName(prefixName + "SessionFactory");
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(packages));
    } catch (IllegalStateException ex) {
        log.info("Could not determine auto-configuration package", ex);
    }
}
Also used : ClassPathMapperScanner(org.mybatis.spring.mapper.ClassPathMapperScanner)

Aggregations

ClassPathMapperScanner (org.mybatis.spring.mapper.ClassPathMapperScanner)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 MapperFactoryBean (org.mybatis.spring.mapper.MapperFactoryBean)1 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)1 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)1