jlefebure / spring-boot-starter-minio

Minio starter for Spring Boot
Apache License 2.0
149 stars 70 forks source link

BeanCurrentlyInCreationException when create minioNotificationConfiguration bean on spring boot 2.6 #22

Open veris4crm opened 2 years ago

veris4crm commented 2 years ago

using version:

com.jlefebure spring-boot-starter-minio 1.10

spring boot version: 2.6.6

problem: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'minioNotificationConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference? at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) at com.jlefebure.spring.boot.minio.MinioNotificationConfiguration.setApplicationContext(MinioNotificationConfiguration.java:63) at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:128) at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:102)

after debuing in MinioNotificationConfiguration.java:63 i found exception on beanName equal MinioNotificationConfiguration self bean.

MusicGun commented 2 years ago
截屏2022-05-02 16 53 28

As exception stack tells, the bug is raised by an 'unresolvable circular reference'.

When spring container is initializing this bean , as this bean implements SpringContextAware , the method setApplicationtContext(ApplicationContext applicationContext) is invoked. However, in the method , author uses a for-loop to get every bean name in the spring context and pass the name to getBean, which will trigger the bean of that beanName to be initialized(if that bean has not been initialized). And this causes the bug, in the for-loop , the beanName of this bean itself will also appear and be passed into getBean. So it's a typical unresolvable circular reference bug.

Though it is not recommended in the setApplicationContext to invoke getBean by Spring because this would trigger some irrelevant beans to be initialized too early .

Still author chooses to do so , maybe because the annotation MinioNotification (actually Asnyc annotation) will cause the target bean to be a proxy bean. And therefore the annotation meta data can not be obtained here. So he/she invokes the getBean method to get target object firstly , and then uses AopUtils to get the real class.

Actually, if you want get meta data from target class , you can use the ClassUtils which will get the parent class of that proxy class (which in CGLIB proxy style is the target class). This is suggested code(:

截屏2022-05-02 17 32 19
Flooze-IT commented 2 years ago

Hi @MusicGun Where do you find the configurableListableBeanFactory ? Thanks ;)

danielcode2020 commented 1 year ago

How to solve this issue? I have the same problem

egeorges92 commented 1 year ago

Hello,

the issue is when beans are scanned, the bean minioNotificationConfiguration is also in the pipe. Add this line at first line of the foreach : ` @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { for (String beanName : applicationContext.getBeanDefinitionNames()) {

        // ignore self
        if("minioNotificationConfiguration".equals(beanName)) {
            continue;
        }

` You may see my pull request: https://github.com/jlefebure/spring-boot-starter-minio/pull/26 It's solved the issue and migrate the starter to spring boot 3.

Enjoy.