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

SpringBoot 3.4.0 Error creating bean with name 'openApiResource' #2819

Open
fangzhengjin opened this issue Dec 18, 2024 · 3 comments
Open

SpringBoot 3.4.0 Error creating bean with name 'openApiResource' #2819

fangzhengjin opened this issue Dec 18, 2024 · 3 comments

Comments

@fangzhengjin
Copy link

Describe the bug

SpringBoot: 3.4.0
SpringDoc: 2.7.0

Application cannot be start

To Reproduce

springdoc:
  enable-default-api-docs: true

Additional context

2024-12-18 14:48:45.153 [SW_CTX: N/A] [pool-2-thread-1] INFO  o.s.b.f.s.DefaultListableBeanFactory -Creating singleton bean 'openApiResource' in thread "pool-2-thread-1" while other thread holds singleton lock for other beans [openApiResource]
Exception in thread "pool-2-thread-1" org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'openApiResource': Requested bean is currently in creation: Is there an unresolvable circular reference or an asynchronous initialization dependency?
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:420)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:281)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:743)
	at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1440)
	at org.springdoc.core.service.OpenAPIService.build(OpenAPIService.java:269)
	at org.springdoc.api.AbstractOpenApiResource.getOpenApi(AbstractOpenApiResource.java:352)
	at org.springdoc.api.AbstractOpenApiResource.lambda$new$0(AbstractOpenApiResource.java:258)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)
@fangzhengjin
Copy link
Author

fangzhengjin commented Dec 18, 2024

Through analysis, the problem should be here https://github.com/springdoc/springdoc-openapi/blob/main/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/OpenAPIService.java#L269

  1. context.getBeansWithAnnotation(RestController.class) Need to obtain org.springdoc.webmvc.api.OpenApiWebMvcResource
  2. org.springdoc.webmvc.api.OpenApiWebMvcResource Need to inject OpenAPIService

It is possible that it is related to the loading logic of the new version of Springboot,Add a breakpoint at context.getBeansWithAnnotation(RestController.class), release it after a while, and everything loads normally.

@bnasslahsen
Copy link
Collaborator

@fangzhengjin,

Not reproducible.
Feel free to provide a Minimal, Reproducible Example - with HelloController that reproduces the problem.

This ticket will be closed, but can be reopened if your provide the reproducible sample.

@fangzhengjin
Copy link
Author

fangzhengjin commented Feb 24, 2025

@bnasslahsen

SpringBoot: 3.4.3
SpringDoc: 2.8.5

This problem may not be reproduced in the smallest sample project, but in a system with more code, this problem occurs and I extracted the exception stack for this problem.

When there are many automatic configurations, it may interfere with the registration order of springdoc-related beans, resulting in this problem.

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'openApiResource': Requested bean is currently in creation: Is there an unresolvable circular reference or an asynchronous initialization dependency?
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:488)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:303)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:769)
	at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1440)
	at org.springdoc.core.service.OpenAPIService.build(OpenAPIService.java:269)
	at org.springdoc.api.AbstractOpenApiResource.getOpenApi(AbstractOpenApiResource.java:352)
	at org.springdoc.api.AbstractOpenApiResource.<init>(AbstractOpenApiResource.java:258)
	at org.springdoc.webmvc.api.OpenApiResource.<init>(OpenApiResource.java:111)
	at org.springdoc.webmvc.api.OpenApiWebMvcResource.<init>(OpenApiWebMvcResource.java:100)
	at org.springdoc.webmvc.core.configuration.SpringDocWebMvcConfiguration.openApiResource(SpringDocWebMvcConfiguration.java:114)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:171)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:88)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:168)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1361)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1191)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:563)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(A

I tried rewriting AbstractOpenApiResource to set getOpenApi to public and configured it using ApplicationReadyEvent in the project's configuration class, and he did it quickly without any errors.

@EventListener
public void onApplicationEvent(ApplicationReadyEvent event) {
    if (springDocConfigProperties.isPreLoadingEnabled()) {
        if (CollectionUtils.isEmpty(springDocConfigProperties.getPreLoadingLocales())) {
            Executors.newSingleThreadExecutor().execute(abstractOpenApiResource::getOpenApi);
        }
        else {
            for (String locale : springDocConfigProperties.getPreLoadingLocales()) {
                Executors.newSingleThreadExecutor().execute(() -> abstractOpenApiResource.getOpenApi(Locale.forLanguageTag(locale)));
            }
        }
    }
}

Using ApplicationReadyEvent in AbstractOpenApiResource causes the same problem.

@bnasslahsen bnasslahsen reopened this Feb 24, 2025
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

No branches or pull requests

2 participants