이 강의만 듣고 얼른 운동 갔다와야지... 🏃🏃🏃
오늘은 최대한 섹션 8까지는 들어보려고 노력을 해야겠다...
슬의생의 읜터가든 보면서 아주 흡족스럽군... 내일부터는 하루에 한편만 봐야지 ㅎㅎㅎ
충돌이 나는 상황
1. 자동 빈 등록 vs 자동 빈 등록
2. 수동 빈 등록 vs 자동 빈 등록
컴포넌트 스캔에서 동일한 이름이 있을 경우
→ ⚠️ ConflictingBeanDefinitionException
에러 발생
➕ 각 컴포넌트 이름을 service로 할 경우
@Component("service")
public class OrderServiceImpl implements OrderService{
@Component("service")
public class MemberServiceImpl implements MemberService{
💻 실행화면
⚠️ 오류 발생!!
org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class [hello.core.AutoAppConfig]; n
ested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'service' for bean class [hello.core.order.OrderServiceImpl]
conflicts with existing, non-compatible bean definition of same name and class
[hello.core.member.MemberServiceImpl]
🤔 수동 빈 등록과 자동 빈 등록에서 빈 이름이 충돌되면 어떻게 될까?
➡️ 수동 빈이 자동빈을 오버라이딩한다.
테스트 해보기 : 자동빈과 이름을 동일하게 수동빈을 만들어보자.
public class AutoAppConfig {
@Bean(name = "memoryMemberRepository")
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
}
💻 실행화면
Overriding bean definition for bean 'memoryMemberRepository'
with a different definition: replacing [Generic bean:
class [hello.core.member.MemoryMemberRepository];
scope=singleton; abstract=false; lazyInit=null;
autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false;
factoryBeanName=null; factoryMethodName=null; initMethodName=null;
destroyMethodName=null; defined in file
👉 이런 애매한 상황...!! 그냥 별로!! ⚠️ 큰 프로젝트에서 이걸 어떻게 오류를 잡을 거냐고!!
⚠️ 스프링 부트는 이제 수동 빈 등록과 자동 빈 등록이 충돌나면 오류가 발생하도록 기본 값을 바꾸었다!!
🔍 스프링 부트인@SpringBootTest class CoreApplicationTests
를 실행하면 오류 발생
➡️ application.properties에서 옵션 변경 가능
spring.main.allow-bean-definition-overriding=true
이런걸 다 외울수는 없다.
애초에 애매한 코드를 쓰지 말자..! "명확하지 않은 건 하지말자."
방대한 스프링을 모두 이해할 수 없고 협업을 위해서는 명확하지 않은 건 만들지 말자.
💭 자동빈등록은 소문자로 빈이름을 설정한다는 규칙을 알아두고, 이런 상황에서 어떻게 할지를 미리 결정해두면
에러를 줄일 수 있을까?
"코드가 길지만 명확" > 코드를 짧은 것
좋은 개발이란 무엇일까...???
갑자기 이런 저런 생각을 하게 되었는데, 현실을 충실히 살면 미래를 기대할 수 있게 되고,
기대가 미래를 상상하는 느낌중 가장 건전한 마음이 아닐까 생각한다.
오늘을 열심히 하면 내일도 열심히 하기 더 쉬워지고, 그러다보면 미래를 마음편하게 기대할 수 있지 않을까 싶다...?
내가 그랬으면 좋겠다~
그래도 운동은 뺴먹으면 체력이 떨어지니까 운동하고 와야겠담~