컴포넌트 스캔을 사용하려면 먼저 @ComponentScan 을 설정 정보에 붙여주기
package hello.core;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import static org.springframework.context.annotation.ComponentScan.*;
@Configuration
@ComponentScan(
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes =
Configuration.class))
public class AutoAppConfig {
}
컴포넌트 스캔은 이름 그대로 @Component 애노테이션이 붙은 클래스를 스캔해서 스프링 빈으로 등록하고, @Component 를 붙여주기
//MemoryMemberRepository @Component 추가
@Component
public class MemoryMemberRepository implements MemberRepository {}
RateDiscountPolicy @Component 추가
@Component
public class RateDiscountPolicy implements DiscountPolicy {}
MemberServiceImpl @Component, @Autowired 추가
@Component
public class MemberServiceImpl implements MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
}
OrderServiceImpl @Component, @Autowired 추가
@Component
public class OrderServiceImpl implements OrderService {
private final MemberRepository memberRepository;
private final DiscountPolicy discountPolicy;
@Autowired
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy
discountPolicy) {
this.memberRepository = memberRepository;
this.discountPolicy = discountPolicy;
}
}
//@Autowired 를 사용하면 생성자에서 여러 의존관계도 한번에 주입받을 수 있다
AutoAppConfigTest.java
package hello.core.scan;
import hello.core.AutoAppConfig;
import hello.core.member.MemberService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.*;
public class AutoAppConfigTest {
@Test
void basicScan() {
ApplicationContext ac = new
AnnotationConfigApplicationContext(AutoAppConfig.class);
MemberService memberService = ac.getBean(MemberService.class);
assertThat(memberService).isInstanceOf(MemberService.class);
}
}
AnnotationConfigApplicationContext 를 사용하는 것은 기존과 동일
설정 정보로 AutoAppConfig 클래스를 넘겨줌
실행해보면 기존과 같이 잘 동작하는 것을 확인 가능
탐색 위치와 기본 스캔 대상
탐색한다. basePackages = {"hello.core", "hello.service"}
이렇게 여러 시작 위치를 지정할 수도 있다.
basePackageClasses : 지정한 클래스의 패키지를 탐색 시작 위치로 지정한다.
만약 지정하지 않으면 @ComponentScan 이 붙은 설정 정보 클래스의 패키지가 시작 위치가 된다.
권장하는 방법 개인적으로 즐겨 사용하는 방법은 패키지 위치를 지정하지 않고,
설정 정보 클래스의 위치를 프로젝트 최상단에 두는 것이다.
최근 스프링 부트도 이 방법을 기본으로 제공한다.
예를 들어서 프로젝트가 다음과 같이 구조가 되어 있으면
com.hello
com.hello.serivce
com.hello.repository
컴포넌트 스캔 기본 대상 컴포넌트 스캔은 @Component 뿐만 아니라
@Component : 컴포넌트 스캔에서 사용
@Controlller : 스프링 MVC 컨트롤러에서 사용
@Service : 스프링 비즈니스 로직에서 사용
@Component
public @interface Controller {
}
@Component
public @interface Service {
}
@Component
public @interface Configuration {
}
'Back-end Skill > Springboot' 카테고리의 다른 글
스프링 세션8_ 빈 생명주기 콜백 (0) | 2022.02.06 |
---|---|
스프링 세션7_의존관계 자동 주입 (0) | 2022.02.06 |
스프링 세션5_ 싱글톤 컨테이너 (0) | 2022.01.31 |
스프링 세션4_스프링 컨테이너와 스프링 빈 정리본 (0) | 2022.01.31 |
스프링 세션 3_ 객체 지향 원리 적용 (0) | 2022.01.31 |