Framework/Springboot

[Springboot] The dependencies of some of the beans in the application context form a cycle 였λ₯˜ / μƒμ„±μž μ£Όμž… μ‹œ μˆœν™˜ μ°Έμ‘° λ°œμƒ

yuri lee 2022. 12. 21. 23:13
λ°˜μ‘ν˜•

Intro

spring bootμ—μ„œ serviceλ₯Ό μ£Όμž…ν•˜μ—¬ μ‚¬μš©ν•˜λ˜ 도쀑 λ‹€μŒμ˜ 였λ₯˜κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. 

 

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

 

μΈν…”λ¦¬μ œμ΄ 터미널에 μˆœν™˜ 참쑰되고 μžˆλ‹€λŠ” ν‘œμ‹œμ™€ ν•¨κ»˜ 잘 μ˜¬λΌκ°€λ˜ μ„œλ²„κ°€ λ™μž‘ν•˜μ§€ μ•Šμ•˜λ”κ΅°μš”..

 

Why?

처음 λ³΄λŠ” 였λ₯˜λΌμ„œ 이게 뭘까 μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€. ꡬ글링을 ν•΄λ³΄λ‹ˆ 두 νŒŒμΌμ—μ„œ μ„œλ‘œ λ™μΌν•˜κ²Œ μƒμ„±μžλ₯Ό μ£Όμž…ν•΄ μ‚¬μš©ν•΄μ„œ μˆœν™˜ μ°Έμ‘°κ°€ μΌμ–΄λ‚˜κ³ , λ•Œλ¬Έμ— μ—λŸ¬κ°€ λ°œμƒν–ˆλ˜ κ²ƒμ΄μ˜€μŠ΅λ‹ˆλ‹€. 

 

How to solve the problem

κ΅¬κΈ€λ§ν•œ κ²°κ³Ό, μˆœν™˜ μ°Έμ‘° 였λ₯˜λ₯Ό ν•΄κ²°ν•  방법은 크게 3가지가 μžˆμ—ˆμŠ΅λ‹ˆλ‹€. 

 

#1 application.yml λ³€κ²½

spring:
  application:
    name: user-service
  main:
    allow-circular-references: true

spring.main.allow-circular-referenceλ₯Ό true둜 μ„€μ •ν•  경우, 잘 λ™μž‘ν•œλ‹€κ³  ν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ 이 방법은 제게 λ™μž‘ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. μŠ€ν”„λ§ 3.0 λ²„μ „μ—μ„œλŠ” λ™μž‘μ„ μ•ˆν•˜λŠ” 건가 μ‹Άλ„€μš”.

#2 Lazy μ–΄λ…Έν…Œμ΄μ…˜ ν™œμš©

@Lazy

κ·Έλž˜μ„œ μ €λŠ” λ‘λ²ˆμ§Έ 방법인 Lazy μ–΄λ…Έν…Œμ΄μ…˜μ„ ν™œμš©ν–ˆμŠ΅λ‹ˆλ‹€. 이 μ–΄λ…Έν…Œμ΄μ…˜μ€ 빈 μ΄ˆκΈ°ν™” μ‹œμ μ—λŠ” ν”„λ‘μ‹œ μ£Όμž… ν›„, 처음 싀행될 λ•Œ μ‹€μ œ 객체λ₯Ό μ£Όμž…ν•œλ‹€κ³  ν•©λ‹ˆλ‹€. 

 

#3 μˆœν™˜ μ°Έμ‘°κ°€ μΌμ–΄λ‚˜μ§€ μ•Šλ„λ‘ ꡬ쑰 λ³€κ²½ 

μ•„μ˜ˆ μˆœν™˜ μ°Έμ‘°κ°€ μΌμ–΄λ‚˜μ§€ μ•Šλ„λ‘ ꡬ쑰λ₯Ό λ³€κ²½ν•˜λŠ” 방법도 μžˆμŠ΅λ‹ˆλ‹€. 

 

Conclusion

상황에 맞게 μ μ ˆν•œ 방법을 μ‚¬μš©ν•˜μ—¬ ν•΄λ‹Ή 였λ₯˜λ₯Ό ν•΄κ²°ν•˜μ‹œκΈΈ λ°”λžλ‹ˆλ‹€. 참고둜 κ°€μž₯ 쒋은 방법은 μˆœν™˜ μ°Έμ‘°κ°€ μΌμ–΄λ‚˜μ§€ μ•Šλ„λ‘ ꡬ쑰λ₯Ό λ³€κ²½ν•˜λŠ” 것이라고 ν•˜λ„€μš”! 

 

 


https://velog.io/@tkaqhcjstk/spring-boot-%EC%98%A4%EB%A5%98%ED%95%B4%EA%B2%B0-The-dependencies-of-some-of-the-beans-in-the-application-context-form-a-cycle

https://appleg1226.tistory.com/16

λ°˜μ‘ν˜•