Skip to content

Cannot scan TypeHandler that pass Class<?> to constructor since 2.0.1 #370

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
ondh opened this issue Jul 20, 2019 · 8 comments
Closed

Cannot scan TypeHandler that pass Class<?> to constructor since 2.0.1 #370

ondh opened this issue Jul 20, 2019 · 8 comments
Assignees
Milestone

Comments

@ondh
Copy link

ondh commented Jul 20, 2019

my code is working fine with mybatis-spring-boot-starter:2.0.0
run application failed after upgrade to 2.1.0

I have tried some test:

working:

  • mybatis-spring-boot-starter 2.0.0 with mybatis.type-handlers-package set in application.properties

not working:

  • mybatis-spring-boot-starter:2.0.1 with mybatis.type-handlers-package set in application.properties
  • mybatis-spring-boot-starter:2.1.0 with mybatis.type-handlers-package set in application.properties

error log are follows:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'. Cause: java.lang.IllegalStateException: No typehandler found for property tags
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1500) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	... 85 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'. Cause: java.lang.IllegalStateException: No typehandler found for property tags
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	... 98 common frames omitted
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'. Cause: java.lang.IllegalStateException: No typehandler found for property tags
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:596) ~[mybatis-spring-2.0.2.jar:2.0.2]
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:475) ~[mybatis-spring-2.0.2.jar:2.0.2]
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:616) ~[mybatis-spring-2.0.2.jar:2.0.2]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:180) ~[mybatis-spring-boot-autoconfigure-2.1.0.jar:2.1.0]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$e3621db3.CGLIB$sqlSessionFactory$1(<generated>) ~[mybatis-spring-boot-autoconfigure-2.1.0.jar:2.1.0]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$e3621db3$$FastClassBySpringCGLIB$$b39dca7c.invoke(<generated>) ~[mybatis-spring-boot-autoconfigure-2.1.0.jar:2.1.0]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$e3621db3.sqlSessionFactory(<generated>) ~[mybatis-spring-boot-autoconfigure-2.1.0.jar:2.1.0]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	... 99 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\Work\Workspace\IdeaProjects\beauty\out\production\resources\mapper\BnItemMapper.xml]'. Cause: java.lang.IllegalStateException: No typehandler found for property tags
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:122) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:94) ~[mybatis-3.5.2.jar:3.5.2]
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:594) ~[mybatis-spring-2.0.2.jar:2.0.2]
	... 112 common frames omitted
Caused by: java.lang.IllegalStateException: No typehandler found for property tags
	at org.apache.ibatis.mapping.ResultMapping$Builder.validate(ResultMapping.java:151) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.mapping.ResultMapping$Builder.build(ResultMapping.java:140) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:391) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:393) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:253) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:245) ~[mybatis-3.5.2.jar:3.5.2]
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:118) ~[mybatis-3.5.2.jar:3.5.2]
	... 114 common frames omitted
@kazuki43zoo
Copy link
Member

Hi @ondh, Could you provide small reproduce project via GitHub?

@ondh
Copy link
Author

ondh commented Jul 20, 2019

Hi @ondh, Could you provide small reproduce project via GitHub?

I have tried the version 2.0.1 is the same error as 2.1.0.

this is demo project with 2 branchs:

https://github.com/ondh/mybatis-boot-demo

@kazuki43zoo
Copy link
Member

@ondh Thanks for your quick feedback!! I will investigate cause of this issue.

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Jul 20, 2019

Hi @ondh ,

Sorry, this is a bug of mybatis-spring. For details, see mybatis/spring#394. This bug will fix at next regular maintenance release(mybatis-spring-boot 2.1.1 and mybatis-spring 2.0.3).

Workaround

I suggest workaround for fix this issue. You can apply a custom type handler to MyBatis by register it into the Spring DI container instead of package scan feature as follow:

package fun.eien.mybatisbootdemo.config.mybatis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisConfig {
  @Bean
  JSONArrayTypeHandler jsonArrayTypeHandler() {
    return new JSONArrayTypeHandler(JSONArray.class);
  }
  @Bean
  JSONObjectTypeHandler jsonObjectTypeHandler() {
    return new JSONObjectTypeHandler(JSONObject.class);
  }
}

FYI, the TypeHandler that define only default constructor can scan using package scan feature.

@kazuki43zoo kazuki43zoo changed the title mybatis.type-handlers-package not working after upgrade to 2.1.0 Cannot scan TypeHandler that pass Class<?> to constructor since 2.0.1 Jul 20, 2019
@kazuki43zoo kazuki43zoo added this to the 2.1.1 milestone Jul 20, 2019
@kazuki43zoo
Copy link
Member

kazuki43zoo commented Jul 20, 2019

In your case(in your reproduce project), I think that not need pass the Class to constructor. In other words, you can resolve this issue by replace to use default constructor instead of current constructor.

@ondh
Copy link
Author

ondh commented Jul 20, 2019

Thanks for your work.

@kazuki43zoo
Copy link
Member

I've fixed via gh-371 and enhance assertion via fe9dfbc.

@kazuki43zoo
Copy link
Member

@ondh I've fixed this issue. Please try to use the 2.1.1-SNAPSHOT at you have a time.

repositories {
    // ...
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

dependencies {
    // ...
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1-SNAPSHOT'
    // ...
}

This issue will release at next maintenance version(about middle Oct 2019).

Thanks again for your contribution!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants