question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SqlSessionFactoryBean falls in circular dependencies by Spring Boot's DataSourceInitializer.

See original GitHub issue

SqlSessionFactoryBean falls in circular dependencies by Spring Boot’s DataSourceInitializer.

With @Lazy annotations it’s okay but without them the following exception occurs:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?

A project with @Lazy annotations: https://github.com/izeye/samples-spring-boot-branches/tree/mybatis-hsqldb

A project without @Lazy annotations: https://github.com/izeye/samples-spring-boot-branches/tree/mybatis-hsqldb-without-lazy

I guess replacing ApplicationListener<ApplicationEvent> with ApplicationListener<ContextRefreshedEvent> would fix it.

If my usage is incorrect, please let me know.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dguggicommented, Mar 2, 2015

I can also reproduce this issue with mybatis-spring-1.2.2, mybatis-3.2.8, spring-boot-1.2.2, spring-4.1.5

My usecase:

this also leads to the warning message mentioned above: “Requested bean is currently in creation: Is there an unresolvable circular reference?”

I think the problem is in MapperFactoryBean.java (mybatis-spring) and how it is created via the “ClassPathMapperScanner”.

my observations:

  • ClassPathMapperScanner creates “GenericBeanDefinition” (in my case for scanned classes that implement a marker interface)
  • afterwards org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean() is invoked for every created “GenericBeanDefinition” (that is MapperFactoryBean instances)
  • … which actually invokes MapperFactoryBean.getObjectType() and this method returns null (!) (as the setMapperInterface() method seems not to be invoked yet)

I could fix this (using a patched mybatis-spring version) as follows:

Changes to MapperFactoryBean.java

add constructor that takes the mapperinterface as argument:

public MapperFactoryBean2(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
 }
  • for backward comp. reasons i also added a default constructor:
public MapperFactoryBean2() {
}

Changes to ClassPathMapperScanner.java

Create GenenericBeanDefinition (in doScan() method) using the new constructur (vs. using setMapperInterface() method):

...
        // the mapper interface is the original class of the bean
        // but, the actual class of the bean is MapperFactoryBean  
definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName());
        // NOTE: the line above replaces the line below...
        //definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());
        definition.setBeanClass(MapperFactoryBean2.class);
...

Hope this helps and could make it into the next mybatis-spring release (as this resolved our issue).

1reaction
emacarroncommented, Jun 20, 2015

You are right! Seems that a later PR changed that lines back. Sorry!

I will remake the change again in master.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular Dependencies in Spring - Baeldung
A quick writeup on dealing with circular dependencies in Spring: how they occur and several ways to work around them.
Read more >
springboot - SqlSessionFactoryBean falls in circular dependencies ...
springboot mybatis配置多数据源的时候,报错:There is a circular dependency between 7 beans in the application con.
Read more >
June 2015 - The MyBatis Blog
SqlSessionFactoryBean falls in circular dependencies by Spring Boot's DataSourceInitializer, see http://github.com/mybatis/spring/issues/58.
Read more >
Cyclic dependency when there are multiple DataSource defined
I created the two "first" and "second" DataSources inside the method which creates the routing datasource so that they don't have to be...
Read more >
Bountysource
SqlSessionFactoryBean falls in circular dependencies by Spring Boot's DataSourceInitializer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found