inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 핵심 원리 - 기본편

컴포넌트 스캔과 의존관계 자동 주입 시작하기

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

331

diaman75

작성한 질문수 7

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 이 붙어 있는 빈들만 등록될 줄 알았는데 왜 이렇게 나오는 걸까요?

 

spring 객체지향

답변 1

1

인프런 AI 인턴

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

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

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

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

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

1

diaman75

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

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

코드 자료

0

51

2

구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?

0

59

2

MemberService의 인터페이스를 왜 사용하는지 궁금합니다.

0

80

1

롬복 @Setter를 써야 하는 상황이 있는건가요?

0

94

1

빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?

0

81

1

테스트 속도가 나중에 영향이 있을까요?

0

77

1

gradle 설정 안떠서 질문 남깁니다!

0

122

2

build.gradle로 프로젝트를 여는 이유

0

87

1

provider 사용하는 이유

0

91

1

다음 강의 뭘 들어야 할까요

0

126

2

프로토타입 빈, 직접 destroy 호출 안 할 경우

0

66

1

beanB

0

82

2

퀴즈다시풀기

0

69

1

Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ

0

92

2

"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.

0

67

3

run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>

0

106

2

도메인의 정의?

0

59

1

ApplicationContext 질문입니다.

0

63

1

@Scope의 proxyMode를 사용할때 단위 테스트 방법

0

91

2

ai api 선정하기 관련 질문

0

119

2

생성자 자동주입 관련해서

0

66

1

생성자 직접 호출 vs 팩토리 메서드 패턴

0

97

2

Spring에서 SessionScope와 RequestScope는 함께 사용되나요?

1

66

1

12:25

0

79

2