코드 그라데이션

필터 본문

Spring/핵심 원리 구현

필터

완벽한 장면 2024. 2. 1. 07:48

필터

 

모든 코드는 테스트 코드에 추가

 

컴포넌트 스캔 대상에 추가할 애노테이션

package inflearn.spring_core.scan.filter;

import java.lang.annotation.*;

// 컴포넌트 스캔 대상에 추가할 애노테이션
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyIncludeComponent {
}

 

 

컴포넌트 스캔 대상에서 제외할 애노테이션

package inflearn.spring_core.scan.filter;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {
}

 

 

컴포넌트 스캔 대상에 추가할 클래스

package inflearn.spring_core.scan.filter;

@MyIncludeComponent
public class BeanA {
}
  • `@MyIncludeComponent` 적용

 

컴포넌트 스캔 대상에서 제외할 클래스

package inflearn.spring_core.scan.filter;

@MyExcludeComponent
public class BeanB {
}
  • `@MyExcludeComponent` 적용

 


설정 정보와 전체 테스트 코드

ComponentFilterAppConfigTest

package inflearn.spring_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 {
    }


}

 

 

 

FilterType 옵션

FilterType은 5가지 옵션이 있다.

 

예를 들어서 BeanA도 빼고 싶으면 다음과 같이 추가하면 된다.

@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` 는 여러가지 이유로 간혹 사용할 때가 있지만 많지는 않다.


특히 최근 스프링 부트는 컴포넌트 스캔을 기본으로 제공하는데, 

개인적으로는 옵션을 변경하면서 사용하기 보다는 

스프링의 기본 설정에 최대한 맞추어 사용하는 것을 권장하고, 선호하는 편이다.

 

728x90

'Spring > 핵심 원리 구현' 카테고리의 다른 글

다양한 의존관계 주입 방법  (1) 2024.02.03
중복 등록과 충돌  (0) 2024.02.02
컴포넌트 스캔 기본 대상  (1) 2024.01.31
탐색 위치  (0) 2024.01.31
컴포넌트 스캔과 의존관계 자동 주입 시작하기  (0) 2024.01.30
Comments