Skip to content
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

When Spring has multiple ApplicationContexts, it may throw BeanCreationNotAllowedException #796

Closed
SkyAndCode opened this issue Mar 28, 2023 · 1 comment · Fixed by #797

Comments

@SkyAndCode
Copy link
Contributor

When there are multiple ApplicationContexts in Spring, ContextClosedEvent may be broadcasted multiple times. For example, FeignContext, since SqlSessionFactory listens to all ApplicationEvents, if the application is closed, when SqlSessionFactoryBean is destroyed first, and FeignContext is destroyed later, ContextClosedEvent will be broadcasted. When Spring tries to get the listener, BeanCreationNotAllowedException is thrown because SqlSessionFactoryBean's singleton bean has been destroyed and it indicates that singletons are currently being destroyed (singletonsCurrentlyInDestruction=true).

From the implementation of SqlSessionFactory, I think that it does not need to listen to all ApplicationEvents, and only needs to listen to ContextRefreshedEvent.

@SkyAndCode
Copy link
Contributor Author

SkyAndCode commented Mar 31, 2023

I noticed that in MyBatis' auto-configuration, the SqlSessionFactoryBean object is not managed by the Spring IoC container. Instead, it is manually created, and the getObject() method is invoked to obtain the SqlSessionFactory at the end. This SqlSessionFactory object is then managed by the IoC container, rather than the SqlSessionFactoryBean.
So, is it incorrect for the SqlSessionFactoryBean object to be managed by the Spring IoC container?

Of course, I still think that using SqlSessionFactoryBean as a listener is too broad in scope for the events it listens to.

Here is the code snippet link for MyBatis automatic configuration.
MybatisAutoConfiguration.java

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

Successfully merging a pull request may close this issue.

1 participant