diff --git a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java index 8dbf1c0314..d824a48af2 100644 --- a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java +++ b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java @@ -460,7 +460,10 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception { Optional.ofNullable(this.vfs).ifPresent(targetConfiguration::setVfsImpl); if (hasLength(this.typeAliasesPackage)) { - scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType) + scanClasses(this.typeAliasesPackage, this.typeAliasesSuperType).stream() + .filter(clazz -> !clazz.isAnonymousClass()) + .filter(clazz -> !clazz.isInterface()) + .filter(clazz -> !clazz.isMemberClass()) .forEach(targetConfiguration.getTypeAliasRegistry()::registerAlias); } @@ -480,6 +483,7 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception { if (hasLength(this.typeHandlersPackage)) { scanClasses(this.typeHandlersPackage, TypeHandler.class).stream() + .filter(clazz -> !clazz.isAnonymousClass()) .filter(clazz -> !clazz.isInterface()) .filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())) .filter(clazz -> ClassUtils.getConstructorIfAvailable(clazz) != null) diff --git a/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java b/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java index 6ac564fb70..4adaf84032 100644 --- a/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java +++ b/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java @@ -21,6 +21,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.util.Properties; +import java.util.UUID; import org.apache.ibatis.cache.impl.PerpetualCache; import org.apache.ibatis.io.JBoss6VFS; @@ -42,6 +43,7 @@ import org.mybatis.spring.type.DummyTypeAlias; import org.mybatis.spring.type.DummyTypeHandler; import org.mybatis.spring.type.SuperType; +import org.mybatis.spring.type.TypeHandlerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -341,13 +343,20 @@ void testAddATypeAlias() throws Exception { @Test void testSearchATypeAliasPackage() throws Exception { setupFactoryBean(); - factoryBean.setTypeAliasesPackage("org.mybatis.spring.type"); + factoryBean.setTypeAliasesPackage("org.mybatis.spring.type, org.mybatis.spring.scan"); TypeAliasRegistry typeAliasRegistry = factoryBean.getObject().getConfiguration().getTypeAliasRegistry(); + System.out.println(typeAliasRegistry.getTypeAliases().keySet()); + assertThat(typeAliasRegistry.getTypeAliases().size()).isEqualTo(81); typeAliasRegistry.resolveAlias("testAlias"); typeAliasRegistry.resolveAlias("testAlias2"); typeAliasRegistry.resolveAlias("dummyTypeHandler"); + typeAliasRegistry.resolveAlias("dummyTypeHandler2"); typeAliasRegistry.resolveAlias("superType"); + typeAliasRegistry.resolveAlias("dummyMapperFactoryBean"); + typeAliasRegistry.resolveAlias("scanclass1"); + typeAliasRegistry.resolveAlias("scanclass2"); + typeAliasRegistry.resolveAlias("scanenum"); } @Test @@ -384,6 +393,7 @@ void testSearchATypeHandlerPackage() throws Exception { TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry(); assertThat(typeHandlerRegistry.hasTypeHandler(BigInteger.class)).isTrue(); assertThat(typeHandlerRegistry.hasTypeHandler(BigDecimal.class)).isTrue(); + assertThat(typeHandlerRegistry.getTypeHandler(UUID.class)).isInstanceOf(TypeHandlerFactory.InnerTypeHandler.class); } @Test @@ -444,4 +454,5 @@ private void assertConfig(SqlSessionFactory factory, String environment, assertThat(factory.getConfiguration().getParameterMapNames().size()).isEqualTo(0); assertThat(factory.getConfiguration().getSqlFragments().size()).isEqualTo(0); } + } diff --git a/src/test/java/org/mybatis/spring/scan/ScanAnnotation.java b/src/test/java/org/mybatis/spring/scan/ScanAnnotation.java new file mode 100644 index 0000000000..13f178a235 --- /dev/null +++ b/src/test/java/org/mybatis/spring/scan/ScanAnnotation.java @@ -0,0 +1,19 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.scan; + +public @interface ScanAnnotation { +} diff --git a/src/test/java/org/mybatis/spring/scan/ScanClass1.java b/src/test/java/org/mybatis/spring/scan/ScanClass1.java new file mode 100644 index 0000000000..5b74b2ac72 --- /dev/null +++ b/src/test/java/org/mybatis/spring/scan/ScanClass1.java @@ -0,0 +1,51 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.scan; + +import java.util.function.Supplier; + +import org.apache.ibatis.jdbc.SQL; + +public class ScanClass1 { + + public static class StaticInnerClass { + + } + + public class InnerClass { + + } + + public enum InnerEnum { + + } + + public @interface InnerAnnotation { + + } + + public String createSqlUsingAnonymousClass() { + return new SQL() {{ + SELECT("a"); + FROM("test1"); + }}.toString(); + } + + public Supplier createSqlSupplier() { + return () -> "SELECT a FROM test1"; + } + +} diff --git a/src/test/java/org/mybatis/spring/scan/ScanClass2.java b/src/test/java/org/mybatis/spring/scan/ScanClass2.java new file mode 100644 index 0000000000..dfd913703b --- /dev/null +++ b/src/test/java/org/mybatis/spring/scan/ScanClass2.java @@ -0,0 +1,51 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.scan; + +import java.util.function.Supplier; + +import org.apache.ibatis.jdbc.SQL; + +public class ScanClass2 { + + public static class StaticInnerClass { + + } + + public class InnerClass { + + } + + public enum InnerEnum { + + } + + public @interface InnerAnnotation { + + } + + public String createSqlUsingAnonymousClass() { + return new SQL() {{ + SELECT("a"); + FROM("test2"); + }}.toString(); + } + + public Supplier createSqlSupplier() { + return () -> "SELECT a FROM test2"; + } + +} diff --git a/src/test/java/org/mybatis/spring/scan/ScanEnum.java b/src/test/java/org/mybatis/spring/scan/ScanEnum.java new file mode 100644 index 0000000000..475df9ac9e --- /dev/null +++ b/src/test/java/org/mybatis/spring/scan/ScanEnum.java @@ -0,0 +1,19 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.scan; + +public enum ScanEnum { +} diff --git a/src/test/java/org/mybatis/spring/scan/ScanInterface.java b/src/test/java/org/mybatis/spring/scan/ScanInterface.java new file mode 100644 index 0000000000..2d9c300a86 --- /dev/null +++ b/src/test/java/org/mybatis/spring/scan/ScanInterface.java @@ -0,0 +1,19 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.scan; + +public interface ScanInterface { +} diff --git a/src/test/java/org/mybatis/spring/scan/package-info.java b/src/test/java/org/mybatis/spring/scan/package-info.java new file mode 100644 index 0000000000..a2c2aa33e9 --- /dev/null +++ b/src/test/java/org/mybatis/spring/scan/package-info.java @@ -0,0 +1,16 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.scan; \ No newline at end of file diff --git a/src/test/java/org/mybatis/spring/type/TypeHandlerFactory.java b/src/test/java/org/mybatis/spring/type/TypeHandlerFactory.java new file mode 100644 index 0000000000..e867f99387 --- /dev/null +++ b/src/test/java/org/mybatis/spring/type/TypeHandlerFactory.java @@ -0,0 +1,81 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.UUID; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedTypes; +import org.apache.ibatis.type.TypeHandler; + +public interface TypeHandlerFactory { + + static TypeHandler handler1() { + return new TypeHandler() { + @Override + public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) { + + } + + @Override + public String getResult(ResultSet rs, String columnName) { + return null; + } + + @Override + public String getResult(ResultSet rs, int columnIndex) { + return null; + } + + @Override + public String getResult(CallableStatement cs, int columnIndex) { + return null; + } + }; + } + + static TypeHandler handler2() { + return new InnerTypeHandler(); + } + + @MappedTypes({UUID.class}) + class InnerTypeHandler implements TypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) { + } + + @Override + public UUID getResult(ResultSet rs, String columnName) { + return null; + } + + @Override + public UUID getResult(ResultSet rs, int columnIndex) { + return null; + } + + @Override + public UUID getResult(CallableStatement cs, int columnIndex) { + return null; + } + + } + +} diff --git a/src/test/java/org/mybatis/spring/type/package-info.java b/src/test/java/org/mybatis/spring/type/package-info.java new file mode 100644 index 0000000000..febad2db54 --- /dev/null +++ b/src/test/java/org/mybatis/spring/type/package-info.java @@ -0,0 +1,16 @@ +/** + * Copyright 2010-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.type; \ No newline at end of file