2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spring Boot エラー The dependencies of some of the beans in the application context form a cycle

Posted at

事象

アプリケーション起動時に以下のエラーが発生

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  testAService defined in file [/Applications/develop/workspace/awba-management/build/classes/kotlin/main/com/awba/manage/awbamanage/service/testAService.class]
↑     ↓
|  testBService defined in file [/Applications/develop/workspace/awba-management/build/classes/kotlin/main/com/awba/manage/awbamanage/service/testBService.class]
└─────┘

原因

testAServiceとtestBServiceクラスでお互いをConstructor Injectionしているため。
Springがどちらのインスタンスを先に作れば良いかわからなくなり、エラーとなる。

testAService.kt
@Service
class TestAService(private val testBService: TestBService){
~
}
testBService.kt
@Service
class TestBService(private val testAService: TestAService){
~
}

解決策

基本的に、お互いを参照するような設計は推奨されてないっぽい。
まずは設計の見直しを検討する。

どうしてもやりたい場合は、@Lazyを使ったりSetter Injectionを使うなどの対応がある

参考

https://www.baeldung.com/circular-dependencies-in-spring
https://stackoverflow.com/questions/40695893/spring-security-circular-bean-dependency

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?