• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

ComponentSacn 의 디폴터 설정에 대한 질문

23.06.15 09:49 작성 23.06.17 09:08 수정 조회수 242

0

[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오) 예
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오) 예
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)

[질문 내용]
안녕하세요? 컴포넌트 스캔에 대한 강의를 듣던 중 궁금해서 테스트를 해보던 중에 이해가 안가는 부분이 있어서 질문을 드립니다.

@Configuration
@ComponentScan
public class AutoAppConfig {
}

강의에서 excludeFilters 설정한 것을 빼고 디폴트 @ComponentScan 을 적용해서 테스트를 해봤습니다.

...

if(beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
System.out.println("beanDefinitionName = " + beanDefinitionName + " value = " + beanDefinition);
}

 

바로 위와 같이 전에 테스트했던 코드를 참고해서 내용을 찍어봤는데...

 

beanDefinitionName = autoAppConfig value = Generic bean: class [hello.core.AutoAppConfig$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null

beanDefinitionName = applicationContextExtendsFindTest.TestConfig value = Generic bean: class [hello.core.beanfind.ApplicationContextExtendsFindTest$TestConfig$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in file [D:\400.Projects\01.SpringCore\core\out\test\classes\hello\core\beanfind\ApplicationContextExtendsFindTest$TestConfig.class]

beanDefinitionName = applicationContextSameBeanFindTest.SameBeanConfig value = Generic bean: class [hello.core.beanfind.ApplicationContextSameBeanFindTest$SameBeanConfig$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in file [D:\400.Projects\01.SpringCore\core\out\test\classes\hello\core\beanfind\ApplicationContextSameBeanFindTest$SameBeanConfig.class]

beanDefinitionName = statefulServiceTest.TestConfig value = Generic bean: class [hello.core.singleton.StatefulServiceTest$TestConfig$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in file [D:\400.Projects\01.SpringCore\core\out\test\classes\hello\core\singleton\StatefulServiceTest$TestConfig.class]

beanDefinitionName = appConfig value = Generic bean: class [hello.core.AppConfig$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in file [D:\400.Projects\01.SpringCore\core\out\production\classes\hello\core\AppConfig.class]

beanDefinitionName = coreApplication value = Generic bean: class [hello.core.CoreApplication$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null; defined in file [D:\400.Projects\01.SpringCore\core\out\production\classes\hello\core\CoreApplication.class]

beanDefinitionName = rateDiscountPolicy value = Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=applicationContextExtendsFindTest.TestConfig; factoryMethodName=rateDiscountPolicy; initMethodNames=null; destroyMethodNames=[(inferred)]; defined in class path resource [hello/core/beanfind/ApplicationContextExtendsFindTest$TestConfig.class]

...

위와 같이 root 패키지 내의 모든 빈이 등록되는 것 같았습니다. 예상으로는 @Component, @Configuration 이 붙어 있는 빈들만 등록될 줄 알았는데 왜 이렇게 나오는 걸까요?

 

답변 1

답변을 작성해보세요.

1

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.06.17

안녕하세요, 인프런 AI 인턴이에요.

@Configuration 어노테이션이 붙은 클래스에 @ComponentScan 어노테이션이 적용될 경우, 지정된 범위 안에서 스프링이 자동으로 컴포넌트를 스캔해서 빈으로 등록합니다. 이때, excludeFilters 설정을 하지 않으면, 일반적으로 스프링이 관리하는 모든 컴포넌트를 스캔하게 됩니다.

따라서, 예시 코드에서 AutoAppConfig 클래스에 @ComponentScan 어노테이션만 있고, excludeFilters 설정이 없으므로, 자동으로 스캔되는 범위는 기본적으로 모든 컴포넌트가 됩니다.

beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION 조건문에서 ROLE_APPLICATION은 스프링에서 자동으로 등록해주는 빈인데, 이는 excludeFilters로 제외시키지 않은 모든 빈을 뜻합니다.

이와 관련해서 질문이 있으시면 추가로 문의해주세요. 감사합니다.

diaman75님의 프로필

diaman75

질문자

2023.06.19

@ComponentScan 관련해서 수업에서 들었는데도 불구하고 잊어버리고 질문을 했네요...excludeFilters 설정이 없으면 @Configuration 설정 자바들이 모두 포함되기 때문에 그걸 제외하려고 추가한다고 강사님이 수업 중 말씀하셨는데...==;;;

질문을 삭제할까 하다가 혹시나 이런 실수를 하시는 분이 또 계실까 싶어 남겨 놓습니다.