인프런 커뮤니티 질문&답변

호춘님의 프로필 이미지

작성한 질문수

스프링 핵심 원리 - 기본편

ComponentScan 동작하지 않는 문제점. 이유가 뭘까요?

20.12.20 02:37 작성

·

159

0

안녕하세요.

ComponentScan 동작하지 않아 문의드립니다.

AutoAppConfig

@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)
public class AutoAppConfig {

}

MemberServiceImpl

@Component
public class MemberServiceImpl implements MemberService{

// App Config 적용 전
// private final MemberRepository memberRepository = new MemoryMemberRepository();

// App Config 적용 후 -> DIP 충족 (interface 에만 의존)
private final MemberRepository memberRepository;

@Autowired // ac.getBean(MemberRepository.class)
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

@Override
public void join(Member member) {
memberRepository.save(member);
}

@Override
public Member findMember(Long memberId) {
return memberRepository.findById(memberId);
}

//테스트 용도
public MemberRepository getMemberRepository() {
return memberRepository;
}
}

MemoryMemberRepository

@Component
public class MemoryMemberRepository implements MemberRepository{

private static Map<Long, Member> store = new HashMap<>();

@Override
public void save(Member member) {
store.put(member.getId(), member);
}

@Override
public Member findById(Long memberId) {
return store.get(memberId);
}
}

AutoAppConfigTest

public class AutoAppConfigTest {
@Test
void basicScan(){
ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);

// MemberService memberService = ac.getBean(MemberService.class);
// assertThat(memberService).isInstanceOf(MemberService.class);

String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println("beanDefinitionName = " + beanDefinitionName);
}
}
}

테스트코드에 대한 결과

02:29:42.506 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@272113c4

02:29:42.523 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'

02:29:42.672 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'

02:29:42.674 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'

02:29:42.675 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'

02:29:42.677 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'

02:29:42.686 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'autoAppConfig'

beanDefinitionName = org.springframework.context.annotation.internalConfigurationAnnotationProcessor

beanDefinitionName = org.springframework.context.annotation.internalAutowiredAnnotationProcessor

beanDefinitionName = org.springframework.context.annotation.internalCommonAnnotationProcessor

beanDefinitionName = org.springframework.context.event.internalEventListenerProcessor

beanDefinitionName = org.springframework.context.event.internalEventListenerFactory

beanDefinitionName = autoAppConfig

컴포넌트가 하나도 스캔되지 않는데, 왜그런걸까요..?

자료와 계속 비교해봐도 모르겠습니다.

답변 2

1

호춘님의 프로필 이미지
호춘
질문자

2020. 12. 20. 13:41

감사합니다. 경로문제가 맞았습니다.

빠른 답변 감사합니다~

0

김영한님의 프로필 이미지
김영한
지식공유자

2020. 12. 20. 11:11

안녕하세요. 호춘님

먼저 AutoAppConfig가 있는 package 위치를 확인해주세요. package 위치가 컴포넌트 스캔을 할 대상 클래스과 같거나 상위 패키지에 있어야 합니다.

만약 그래도 잘 되지 않으면 전체 프로젝트를 압축해서 올려주세요.

감사합니다.

호춘님의 프로필 이미지

작성한 질문수

질문하기