728x90
@ComponentScan 은 @Component가 붙은 모든 클래스를 스프링 빈으로 등록한다
이때 스프링 빈의 기본 이름은 클래스명을 사용하되 맨 앞글자만 소문자를 사용한다.
* 빈 이름 기본 전략 : MemberServiceImpl 클래스 -> memberServiceImpl
* 빈 이름 직접 지정 : 만약 스프링 빈의 이름을 직접 지정하고 싶으면 @Component("지정할 이름")
@Component 어노테이션 붙은 클래스 스캔
@Configuration
@ComponentScan
public class AutoAppConfig {
}
@Component
public class MemberServiceImpl implements MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
AutoAppConfig 스프링 컨테이너에 등록해 빈 사용
public class AutoAppConfigTest {
@Test
void basicScan()
{
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
MemberService memberService = ac.getBean(MemberService.class);
Assertions.assertThat(memberService).isInstanceOf(MemberService.class);
}
}728x90
'백엔드 > Spring(Boot)' 카테고리의 다른 글
| @Autowired 옵션 (0) | 2021.08.13 |
|---|---|
| 컴포넌트 스캔 필터 (@ComponentScan) (0) | 2021.08.12 |
| ApplicationContext란 (0) | 2021.08.10 |
| Bean 조회 (0) | 2021.08.10 |
| 스프링 빈 확인 및 출력 (0) | 2021.08.10 |