컴포넌트 스캔
- 스프링은 자동으로 스프링 빈을 등록하는 컴포넌트 스캔이라는 기능을 제공한다
- 컴포넌트 스캔을 사용하려면 @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
// @Component붙은 애들을 쫙 스캔해옴
@ComponentScan(
// 기존 Configuration까지 스캔해오지 않도록 제외해줌(실제에서는 잘 안씀)
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes =
Configuration.class))
public class AutoAppConfig {
}
- @ComponentScan은 @Component 어노테이션이 붙은 클래스를 스캔해서 스프링 빈으로 등록한다
- 의존관계를 자동으로 주입해주는 @Autowired라는 기능도 제공한다
@ComponentScan
- @Component가 붙은 모든 클래스를 스프링 빈으로 등록한다
- 스프링 빈의 이름 디폴트 값은 클래스 명 파스칼 -> 카멜 표기법으로만 변경.
- 직접 이름을 지정하고 싶을 경우(비추): @Component("memberService22") 이렇게.
@Component
public class MemoryMemberRepository implements MemberRepository {}
@Autowired
- 생성자에 지정하면 스프링 컨테이너가 자동으로 스프링 빈을 찾아서 주입한다.
- 타입이 같은 빈을 찾아서 주입한다 (getBean(MemberRepository.class))
- memoryMemberRepository도 memberRepository의 자손이므로 같은 타입으로 인식.
- 여러개의 파라미터도 다 찾아서 자동으로 주입한다.
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
탐색 위치와 기본 스캔 대상
- basePackages: 탐색할 패키지의 시작 위치를 지정한다. 이 패키지를 포함한 하위 패키지를 모두 탐색한다. 여러 시작 위치를 지정할 수도 있다.
- basePackageClasses: 지정한 클래스의 패키지를 탐색 시작 위치로 지정한다.
- 지정하지 않으면 @ComponentScan이 붙은 설정 정보 클래스의 패키지가 시작 위치
** 패키지 위치를 지정하지 않고 설정 정보 클래스의 위치를 프로젝트 최상단에 두면 편함.
@ComponentScan(
basePackages = "hello.core.member",
)
컴포넌트 스캔 기본 대상
- @Component: 컴포넌트 스캔에서 사용
- @Controller: 스프링 MVC 컨트롤러에서 사용
-> 스프링 MVC 컨트롤러로 인식
- @Service: 스프링 비즈니스 로직에서 사용
-> 특별한 처리 없음. 개발자가 핵심 비즈니스 로직을 인식하는데 도움을 줄 뿐이다.
- @Repository: 스프링 데이터 접근 계층에서 사용
-> 스프링 데이터 접근 계층으로 인식, 데이터 계층의 예외를 스프링 예외로 변환해준다.
- @Configuration: 스프링 설정 정보에서 사용
-> 스프링 설정 정보로 인식, 스프링 빈이 싱글톤을 유지하도록 추가 처리를 함
- 각각은 전부 @Component를 포함하고 있다.
필터
- includeFilters : 컴포넌트 스캔 대상을 추가로 지정
- excludeFilters : 컴포넌트 스캔에서 제외할 대상을 지정
package hello.core.scan.filter;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {
}
package hello.core.scan.filter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.context.annotation.ComponentScan.Filter;
public class ComponentFilterAppConfigTest {
@Test
void filterScan() {
ApplicationContext ac = new
AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
BeanA beanA = ac.getBean("beanA", BeanA.class);
assertThat(beanA).isNotNull();
Assertions.assertThrows(
NoSuchBeanDefinitionException.class,
() -> ac.getBean("beanB", BeanB.class));
}
@Configuration
@ComponentScan(
includeFilters = @Filter(type = FilterType.ANNOTATION, classes =
MyIncludeComponent.class),
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes =
MyExcludeComponent.class)
)
static class ComponentFilterAppConfig {
}
}
FilterType 옵션
- ANNOTATION: 기본값, 애노테이션을 인식해서 동작한다.
ex) org.example.SomeAnnotation
- ASSIGNABLE_TYPE: 지정한 타입과 자식 타입을 인식해서 동작한다.
ex) org.example.SomeClass
- ASPECTJ: AspectJ 패턴 사용
ex) org.example..*Service+
- REGEX: 정규 표현식
ex) org\.example\.Default.*
- CUSTOM: TypeFilter 이라는 인터페이스를 구현해서 처리
ex) org.example.MyTypeFilter
** @Component면 충분하기 때문에 굳이 사용할 필요는 없다
'Spring > 이론' 카테고리의 다른 글
웹 애플리케이션 이해 (0) | 2022.07.14 |
---|---|
빈 스코프(Bean Scope)란? (0) | 2022.07.04 |
싱글톤 컨테이너(Spring) (0) | 2022.07.04 |
제어의 역전(IoC) & 의존관계 주입(DI) (0) | 2022.06.30 |
SOLID(좋은 객체 지향 설계의 5가지 원칙) (0) | 2022.06.29 |