Skip to content

To enhance the performance and support wildcards #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.util.ClassUtils;

/**
* {@code FactoryBean} that creates an MyBatis {@code SqlSessionFactory}.
Expand Down Expand Up @@ -191,7 +197,7 @@ public void setPlugins(Interceptor[] plugins) {
}

/**
* Packages to search for type aliases.
* Packages to search for type aliases,wildcards are supported,such as a.b.**.c.
*
* @since 1.0.1
*
Expand Down Expand Up @@ -390,7 +396,7 @@ public void afterPropertiesSet() throws Exception {
* @return SqlSessionFactory
* @throws IOException if loading the config file failed
*/
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
protected SqlSessionFactory buildSqlSessionFactory() throws Exception {

Configuration configuration;

Expand Down Expand Up @@ -431,12 +437,24 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray) {
configuration.getTypeAliasRegistry().registerAliases(packageToScan,
typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(packageToScan) + "/**/*.class";
ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
Resource[] resources = rpr.getResources(pattern);
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(rpr);
for (Resource resource : resources) {
if (resource.isReadable()) {
ClassMetadata classMetadata = readerFactory.getMetadataReader(resource).getClassMetadata();
Class clazz = Class.forName(classMetadata.getClassName());
if (typeAliasesSuperType == null || typeAliasesSuperType.isAssignableFrom(clazz)){
configuration.getTypeAliasRegistry().registerAlias(clazz);
}
}
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
}

if (!isEmpty(this.typeAliases)) {
Expand Down