· includeFilters : 컴포넌트 스캔 대상을 추가로 지정
· excludeFilters : 컴포넌트 스캔에서 제외할 대상을 지정
1. 컴포넌트 스캔 대상에 추가할 애노테이션
- src/test/java/hello/core/scan/filter/MyIncludeComponent.java
package hello.core.scan.filter;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyIncludeComponent {
}
2. 컴포넌트 스캔 대상에서 제외할 애노테이션
- src/test/java/hello/core/scan/filter/MyExcludeComponent.java
package hello.core.scan.filter;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {
}
3. 컴포넌트 스캔 대상에 추가할 클래스
- src/test/java/hello/core/scan/filter/BeanA.java
package hello.core.scan.filter;
@MyIncludeComponent
public class BeanA {
}
@MyIncludeComponent 적용
4. 컴포넌트 스캔 대상에서 제외할 클래스
- src/test/java/hello/core/scan/filter/BeanB.java
package hello.core.scan.filter;
@MyExcludeComponent
public class BeanB {
}
@MyExcludeComponent 적용
5. 설정 정보와 전체 테스트 코드
- src/test/java/hello/core/scan/filter/ComponentFilterAppConfigTest.java
package hello.core.scan.filter;
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.junit.jupiter.api.Assertions.assertThrows;
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();
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 {
}
}
includeFilters 에 @MyIncludeComponent 애노테이션 추가해서 BeanA 가 스프링 빈에 등록된다.
excludeFilters 에 @MyExcludeComponent 애노테이션 추가해서 BeanB 가 스프링 빈에 등록되지 않는다.
6. FilterType 옵션
5가지 옵션이 존재
· 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
BeanA도 빼고 싶으면 다음과 같이 추가
@Configuration
@ComponentScan(
includeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class)
, excludeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
, @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = BeanA.class)
}
)
@Component 면 충분하기 때문에,
includeFilters 를 사용할 일은 거의 없다.
excludeFilters 는 여러가지 이유로 간혹 사용할 때가 있지만 많지 않다.
스프링 부트는 컴포넌트 스캔을 기본으로 제공한다.
옵션을 변경하기 보다 스프링 기본 설정에 최대한 맞추어 사용하는 것을 권장한다.
참고링크 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8?cid=325969
스프링 핵심 원리 - 기본편| 김영한 - 인프런 강의
현재 평점 5.0점 수강생 49,558명인 강의를 만나보세요. 스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 스프링 기본 기능, 스프
www.inflearn.com
'강의 실습 > 스프링 핵심 원리 - 기본편' 카테고리의 다른 글
| 다양한 의존관계 주입 방법 (0) | 2026.04.24 |
|---|---|
| 중복 등록과 충돌 (0) | 2026.04.23 |
| 탐색 위치와 기본 스캔 대상 (0) | 2026.04.21 |
| 컴포넌트 스캔과 의존관계 자동 주입 시작하기 (0) | 2026.04.20 |
| @Configuration과 바이트코드 조작의 마법 (1) | 2026.04.18 |
댓글