Skip to content

Commit dd5a9ff

Browse files
committed
Allow to scan TypeHandler that pass Class<?> to constructor
Fixes gh-394
1 parent 03c62f3 commit dd5a9ff

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void setDatabaseIdProvider(DatabaseIdProvider databaseIdProvider) {
186186

187187
/**
188188
* Gets the VFS.
189-
*
189+
*
190190
* @return a specified VFS
191191
*/
192192
public Class<? extends VFS> getVfs() {
@@ -195,7 +195,7 @@ public Class<? extends VFS> getVfs() {
195195

196196
/**
197197
* Sets the VFS.
198-
*
198+
*
199199
* @param vfs
200200
* a VFS
201201
*/
@@ -205,7 +205,7 @@ public void setVfs(Class<? extends VFS> vfs) {
205205

206206
/**
207207
* Gets the Cache.
208-
*
208+
*
209209
* @return a specified Cache
210210
*/
211211
public Cache getCache() {
@@ -214,7 +214,7 @@ public Cache getCache() {
214214

215215
/**
216216
* Sets the Cache.
217-
*
217+
*
218218
* @param cache
219219
* a Cache
220220
*/
@@ -270,7 +270,7 @@ public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) {
270270
*
271271
* <p>
272272
* Since 2.0.1, allow to specify a wildcard such as {@code com.example.*.typehandler}.
273-
*
273+
*
274274
* @since 1.0.1
275275
*
276276
* @param typeHandlersPackage
@@ -331,7 +331,7 @@ public void setConfigLocation(Resource configLocation) {
331331

332332
/**
333333
* Set a customized MyBatis configuration.
334-
*
334+
*
335335
* @param configuration
336336
* MyBatis configuration
337337
* @since 1.3.0
@@ -535,7 +535,6 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
535535
if (hasLength(this.typeHandlersPackage)) {
536536
scanClasses(this.typeHandlersPackage, TypeHandler.class).stream().filter(clazz -> !clazz.isAnonymousClass())
537537
.filter(clazz -> !clazz.isInterface()).filter(clazz -> !Modifier.isAbstract(clazz.getModifiers()))
538-
.filter(clazz -> ClassUtils.getConstructorIfAvailable(clazz) != null)
539538
.forEach(targetConfiguration.getTypeHandlerRegistry()::register);
540539
}
541540

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2010-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.core.jdk.type;
17+
18+
import java.sql.CallableStatement;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.util.concurrent.atomic.AtomicInteger;
23+
import java.util.concurrent.atomic.AtomicLong;
24+
25+
import org.apache.ibatis.type.JdbcType;
26+
import org.apache.ibatis.type.MappedTypes;
27+
import org.apache.ibatis.type.TypeHandler;
28+
29+
@MappedTypes({ AtomicInteger.class, AtomicLong.class })
30+
public class AtomicNumberTypeHandler implements TypeHandler<Number> {
31+
32+
public AtomicNumberTypeHandler(Class<?> type) {
33+
}
34+
35+
@Override
36+
public void setParameter(PreparedStatement ps, int i, Number parameter, JdbcType jdbcType) throws SQLException {
37+
}
38+
39+
@Override
40+
public Number getResult(ResultSet rs, String columnName) throws SQLException {
41+
return null;
42+
}
43+
44+
@Override
45+
public Number getResult(CallableStatement cs, int columnIndex) throws SQLException {
46+
return null;
47+
}
48+
49+
@Override
50+
public Number getResult(ResultSet rs, int columnIndex) throws SQLException {
51+
return null;
52+
}
53+
54+
}

src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.math.BigInteger;
2323
import java.util.Properties;
2424
import java.util.UUID;
25+
import java.util.concurrent.atomic.AtomicInteger;
26+
import java.util.concurrent.atomic.AtomicLong;
2527

2628
import org.apache.ibatis.cache.impl.PerpetualCache;
2729
import org.apache.ibatis.io.JBoss6VFS;
@@ -43,6 +45,7 @@
4345
import org.apache.ibatis.type.TypeHandler;
4446
import org.apache.ibatis.type.TypeHandlerRegistry;
4547
import org.junit.jupiter.api.Test;
48+
import org.mybatis.core.jdk.type.AtomicNumberTypeHandler;
4649
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
4750
import org.mybatis.spring.type.DummyTypeAlias;
4851
import org.mybatis.spring.type.DummyTypeHandler;
@@ -400,12 +403,14 @@ void testSearchATypeAliasPackageWithSamePackage() throws Exception {
400403
@Test
401404
void testSearchATypeHandlerPackage() throws Exception {
402405
setupFactoryBean();
403-
factoryBean.setTypeHandlersPackage("org.**.type");
406+
factoryBean.setTypeHandlersPackage("org.mybatis.**.type");
404407

405408
TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
406409
assertThat(typeHandlerRegistry.hasTypeHandler(BigInteger.class)).isTrue();
407410
assertThat(typeHandlerRegistry.hasTypeHandler(BigDecimal.class)).isTrue();
408411
assertThat(typeHandlerRegistry.getTypeHandler(UUID.class)).isInstanceOf(TypeHandlerFactory.InnerTypeHandler.class);
412+
assertThat(typeHandlerRegistry.getTypeHandler(AtomicInteger.class)).isInstanceOf(AtomicNumberTypeHandler.class);
413+
assertThat(typeHandlerRegistry.getTypeHandler(AtomicLong.class)).isInstanceOf(AtomicNumberTypeHandler.class);
409414
}
410415

411416
@Test

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy