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

Bean 네이밍의 충돌 #4

Open
CODe5753 opened this issue Apr 16, 2022 · 0 comments
Open

Bean 네이밍의 충돌 #4

CODe5753 opened this issue Apr 16, 2022 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@CODe5753
Copy link
Owner

CODe5753 commented Apr 16, 2022

What

Bean 네이밍은 크게 두 가지가 있다.

자동 네이밍

@Component // 자동으로 "naming"으로 빈 네임이 설정된다 (lowerCamelCase)
public class Naming { }

수동 네이밍

@Component("naming")
public class NoName { }

스프링 부트 환경인 곳과 아닌 곳에서 위 네이밍이 충돌하는 경우에 어떻게 쓰이는지 살펴보자.

스프링 부트가 아닌 환경

정상실행되지만 Overriding 되는 것을 볼 수 있다

01:04:19.317 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 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; 
... 생략

스프링 부트

런타임시에 에러가 발생하고 아예 종료된다

image

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

위 메세지를 보면 default option으로 오버라이딩이 false인 것을 알 수 있고, 오버라이딩 기능을 원하는 경우 옵션을 변경할 수 있다.

결론

수동 네이밍과 자동 네이밍이 충돌하는 경우는 절대 의도한 경우는 아닐 것이고 개발자의 실수인 경우가 대부분일 것이라 예상된다.
즉, 오버라이딩된 경우도 의도한 경우가 아닌데 실행에는 문제가 없다는 것은 큰 문제를 야기할 수 있다.

@CODe5753 CODe5753 added the documentation Improvements or additions to documentation label Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant