All Downloads are FREE. Search and download functionalities are using the official Maven repository.

titan.lightbatis.scan.ClassPathMapperScanner Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package titan.lightbatis.scan;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.env.Environment;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.StringUtils;
import titan.lightbatis.configuration.MapperConfig;
import titan.lightbatis.exception.LightbatisException;
import titan.lightbatis.mapper.SpringBootBindUtil;
import titan.lightbatis.mybatis.LightbatisFactoryBean;
import titan.lightbatis.mybatis.MapperBuilder;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;

/**
 * A {@link ClassPathBeanDefinitionScanner} that registers Mappers by
 * {@code basePackage}, {@code annotationClass}, or {@code markerInterface}. If
 * an {@code annotationClass} and/or {@code markerInterface} is specified, only
 * the specified types will be searched (searching for all interfaces will be
 * disabled).
 * 

* This functionality was previously a private class of * {@link MapperScannerConfigurer}, but was broken out in version 1.2.0. * * @author Hunter Presnall * @author Eduardo Macarron * @since 1.2.0 */ public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner { private boolean addToConfig = true; private SqlSessionFactory sqlSessionFactory; private SqlSessionTemplate sqlSessionTemplate; private String sqlSessionTemplateBeanName; private String sqlSessionFactoryBeanName; private Class annotationClass; private Class markerInterface; private MapperBuilder mapperBuilder; private String mapperHelperBeanName; private LightbatisFactoryBean mapperFactoryBean = new LightbatisFactoryBean(); public ClassPathMapperScanner(BeanDefinitionRegistry registry) { super(registry, false); } /** * Configures parent scanner to search for the right interfaces. It can search * for all interfaces or just for those that extends a markerInterface or/and * those annotated with the annotationClass */ public void registerFilters() { boolean acceptAllInterfaces = true; // if specified, use the given annotation and / or marker interface 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 protected boolean matchClassName(String className) { return false; } }); acceptAllInterfaces = false; } if (acceptAllInterfaces) { // default include filter that accepts all classes addIncludeFilter(new TypeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return true; } }); } // exclude package-info.java addExcludeFilter(new TypeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { String className = metadataReader.getClassMetadata().getClassName(); if(className.endsWith("package-info")){ return true; } return metadataReader.getAnnotationMetadata() .hasAnnotation("titan.lightbatis.annotations.RegisterMapper"); } }); } /** * Calls the parent search that will search and register all the candidates. * Then the registered objects are post processed to set them as * MapperFactoryBeans */ @Override public Set doScan(String... basePackages) { Set beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration."); } else { processBeanDefinitions(beanDefinitions); } return beanDefinitions; } private void processBeanDefinitions(Set beanDefinitions) { GenericBeanDefinition definition; for (BeanDefinitionHolder holder : beanDefinitions) { definition = (GenericBeanDefinition) holder.getBeanDefinition(); if (logger.isDebugEnabled()) { logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); } this.mapperBuilder.registerMapper(definition.getBeanClassName()); // the mapper interface is the original class of the bean // but, the actual class of the bean is MapperFactoryBean definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59 definition.setBeanClass(this.mapperFactoryBean.getClass()); //设置通用 Mapper if(StringUtils.hasText(this.mapperHelperBeanName)){ definition.getPropertyValues().add("mapperBuilder", new RuntimeBeanReference(this.mapperHelperBeanName)); } else { //不做任何配置的时候使用默认方式 if(this.mapperBuilder == null){ this.mapperBuilder = new MapperBuilder(); this.mapperBuilder.ifEmptyRegisterDefaultInterface(); } definition.getPropertyValues().add("mapperBuilder", this.mapperBuilder); } definition.getPropertyValues().add("addToConfig", this.addToConfig); boolean explicitFactoryUsed = false; if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionFactory != null) { definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory); explicitFactoryUsed = true; } 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; } else if (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); } } } /** * {@inheritDoc} */ @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent(); } /** * {@inheritDoc} */ @Override protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) { if (super.checkCandidate(beanName, beanDefinition)) { return true; } else { logger.warn("Skipping MapperFactoryBean with name '" + beanName + "' and '" + beanDefinition.getBeanClassName() + "' mapperInterface" + ". Bean already defined with the same name!"); return false; } } public MapperBuilder getMapperBuilder() { return mapperBuilder; } public void setMapperBuilder(MapperBuilder mapperBuilder) { this.mapperBuilder = mapperBuilder; } public void setAddToConfig(boolean addToConfig) { this.addToConfig = addToConfig; } public void setAnnotationClass(Class annotationClass) { this.annotationClass = annotationClass; } /** * 配置通用 Mapper * * @param config */ public void setConfig(MapperConfig config) { if (mapperBuilder == null) { mapperBuilder = new MapperBuilder(); } mapperBuilder.setConfig(config); } public void setMapperFactoryBean(LightbatisFactoryBean mapperFactoryBean) { this.mapperFactoryBean = mapperFactoryBean != null ? mapperFactoryBean : new LightbatisFactoryBean(); } public void setMapperHelperBeanName(String mapperHelperBeanName) { this.mapperHelperBeanName = mapperHelperBeanName; } /** * 从环境变量中获取 mapper 配置信息 * * @param environment */ public void setMapperProperties(Environment environment) { MapperConfig config = SpringBootBindUtil.bind(environment, MapperConfig.class, MapperConfig.PREFIX); if (mapperBuilder == null) { mapperBuilder = new MapperBuilder(); } if(config != null){ mapperBuilder.setConfig(config); } mapperBuilder.ifEmptyRegisterDefaultInterface(); } /** * 从 properties 数组获取 mapper 配置信息 * * @param properties */ public void setMapperProperties(String[] properties) { if (mapperBuilder == null) { mapperBuilder = new MapperBuilder(); } Properties props = new Properties(); for (String property : properties) { property = property.trim(); int index = property.indexOf("="); if(index < 0){ throw new LightbatisException("通过 @Lightbatis 注解的 properties 参数配置出错:" + property + " !\n" ); } props.put(property.substring(0, index).trim(), property.substring(index + 1).trim()); } mapperBuilder.setProperties(props); } public void setMarkerInterface(Class markerInterface) { this.markerInterface = markerInterface; } public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } public void setSqlSessionFactoryBeanName(String sqlSessionFactoryBeanName) { this.sqlSessionFactoryBeanName = sqlSessionFactoryBeanName; } public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } public void setSqlSessionTemplateBeanName(String sqlSessionTemplateBeanName) { this.sqlSessionTemplateBeanName = sqlSessionTemplateBeanName; } }