Spring boot application fails to start after upgrading to 2.6.0 due to circular dependency[ unresolvable circular reference] [closed]
Asked Answered
C

1

30

Spring boot upgrade Error

Spring boot application fails to start after upgrading to 2.6.0 due to circular dependency

Error creating bean with name 'securityConfig': 
Requested bean is currently in creation: Is there an 
      unresolvable circular reference?
Commence answered 19/11, 2021 at 15:5 Comment(1)
Duplicate. See #70074248Murtagh
C
67

Circular References Prohibited by Default in spring boot version 2.6

If your application fails to start due to a BeanCurrentlyInCreationException you are strongly encouraged to update your configuration to break the dependency cycle.

The temporary solution is to restore 2.5’s behavior, set the following in .properties/yml,

spring: 
  main:
    allow-circular-references: true

See this article for an in-depth description and some possible remedies.

Commence answered 19/11, 2021 at 15:5 Comment(4)
spring never allowed circular dependency, how come you say "to restore 2.5's behavior"?Mummify
Circular dependencies are often not an issue if you use field injection. They're only a problem if you either a) use constructor injection or b) have a container-triggered method (such as @PostConstruct) in more than one member of the cycle that accesses an @Autowired field. Other than that, circular dependencies between beans are harmless.Peyton
I worked on a project (Spring-Boot 2.5) with circular dependencies which were really 'circular' and workarounds such as using @Lazy won't help. However, such 'loops' (stacks) of execution never happened in real life (there were no such method calls in the application which could cause real "loop"). Bad design, hard to troubleshoot, risky to leave it "as is". However, many unit tests were failing in v.2.5. But production didn't fail. v.2.5 didn't allow circular dependencies at run time; v.2.6 added validation before application fully initializes, before run time.Tideway
I have tried to use it in application.properties file like this "spring.main.allow-circular-references=true" and not worksSisson

© 2022 - 2024 — McMap. All rights reserved.