During the writing of the previous article A real world example of Spring + MyBatis + Infinispan: part 2, I had trouble setting mybatis-spring up. Even if the problem and the solution are not completely clear to me, maybe to write some notes down could be useful for the future.

I have found myself looking at the following kind of log too many times, during the application development first phase.

(I was using Java 8, Spring 4.2.1, Mybatis 3.3.0, Mybatis-Spring 1.2.3, MyBatis Generator 1.3.3-snapshot, PostgreSQL 9.1).

AVVERTENZA: Bean creation exception on FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vbitaaRoaCustomMapper' defined in file [C:\Users\16800028\ws-sts\bit3-lib\bit-common-db\target\classes\it\caladyon\db\mapper\handmade\bita\VbitaaRoaCustomMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : Error creating bean with name 'sqlSessionFactory' defined in class it.caladyon.bit.lib.dao_spring.config.MybatisSpringConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConf': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String it.caladyon.bit.hp.inc.config.DatabaseConf.ds; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'DatabaseConf.datasource' in string value "${DatabaseConf.datasource}"; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class it.caladyon.bit.lib.dao_spring.config.MybatisSpringConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConf': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String it.caladyon.bit.hp.inc.config.DatabaseConf.ds; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'DatabaseConf.datasource' in string value "${DatabaseConf.datasource}"
ott 14, 2015 2:16:07 PM org.springframework.beans.factory.support.AbstractBeanFactory getTypeForFactoryBean
AVVERTENZA: Bean creation exception on FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vbitaaRoaCustomMapper' defined in file [C:\Users\16800028\ws-sts\bit3-lib\bit-common-db\target\classes\it\caladyon\db\mapper\handmade\bita\VbitaaRoaCustomMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
ott 14, 2015 2:16:07 PM org.springframework.beans.factory.support.AbstractBeanFactory getTypeForFactoryBean
AVVERTENZA: Bean creation exception on FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vbitaaRoaCustomMapper' defined in file [C:\Users\16800028\ws-sts\bit3-lib\bit-common-db\target\classes\it\caladyon\db\mapper\handmade\bita\VbitaaRoaCustomMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
ott 14, 2015 2:16:07 PM org.springframework.beans.factory.support.AbstractBeanFactory getTypeForFactoryBean

This sentence seems important: “Bean creation exception on FactoryBean … UnsatisfiedDependencyException: Error creating bean with name ‘vbitaaRoaCustomMapper’ …: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’: : Error creating bean with name ‘sqlSessionFactory’: Requested bean is currently in creation: Is there an unresolvable circular reference?

I believed that sqlSessionFactory needed the mapper that needed sqlSessionFactory itself.So I put the @Lazy annotation on the sqlSessionFactory bean, and the error didn’t show anymore!

But I was “fooled”. The error is elsewhere and showed up again the very next day!

The real important part of the log is: “Could not autowire field: private java.lang.String it.caladyon.bit.hp.inc.config.DatabaseConf.ds”.

I don’t know why, but an error on another bean instantiation is recognized (or simply logged) by mybatis-spring as a circular dependency between the SqlSessionFactoryBean and the mappers.

Also I don’t know why @Lazy seemed to solve the issue in the first place.

Annunci