/* ์ด ๊ธ์ ๊น์ํ๋์ ๊ฐ์๋ฅผ ๋ณด๊ณ ์ ๋ฆฌํ๋ ค๊ณ ์์ฑํ ๊ธ์ ๋๋ค.
๊ฐ์ธ์ ์ธ ๊ณต๋ถ๋ฅผ ์ํด ์ฌ๋ฆฌ๋ ๊ธ์ด๋ฏ๋ก ์ค๊ฐ ์ค๊ฐ ์ฝ๋๋ ์๋ต๋์์ต๋๋ค. */
์คํ๋ง ํต์ฌ ์๋ฆฌ - ๊ธฐ๋ณธํธ - ์ธํ๋ฐ | ๊ฐ์
์คํ๋ง ์ ๋ฌธ์๊ฐ ์์ ๋ฅผ ๋ง๋ค์ด๊ฐ๋ฉด์ ์คํ๋ง์ ํต์ฌ ์๋ฆฌ๋ฅผ ์ดํดํ๊ณ , ์คํ๋ง ๊ธฐ๋ณธ๊ธฐ๋ฅผ ํ์คํ ๋ค์ง ์ ์์ต๋๋ค., ์คํ๋ง ํต์ฌ ์๋ฆฌ๋ฅผ ์ดํดํ๊ณ , ์ฑ์ฅํ๋ ๋ฐฑ์๋ ๊ฐ๋ฐ์๊ฐ ๋์ด๋ณด์ธ์! ๐ข
ํํฐ
1. ํํฐ ์ข ๋ฅ
โ includeFilters : ์ปดํฌ๋ํธ ์ค์บ ๋์์ ์ถ๊ฐ๋ก ์ง์ ํ๋ค.
โ excludeFilters : ์ปดํฌ๋ํธ ์ค์บ์์ ์ ์ธํ ๋์์ ์ง์ ํ๋ค.
2. ํ ์คํธ ์ฝ๋ ์์ฑ
MyIncludeComponent
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyIncludeComponent {
}
BeanA
@MyIncludeComponent
public class BeanA {
}
MyExcludeComponet
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {
}
BeanB
@MyExcludeComponent
public class BeanB {
}
ComponentFilterAppConfigTest
public class ComponentFilterAppConfigTest {
@Test
void filterScan() {
ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
BeanA beanA = ac.getBean("beanA", BeanA.class);
Assertions.assertThat(beanA).isNotNull();
BeanB beanB = 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 {
}
}
โ ์ ์ฒ๋ผ ํ ์คํธ ์ `NoSuchBeanDefinitionException` ์ค๋ฅ ๋ฐ์.
(excludeFilters์ MyExcludeCompnent ์ ๋ ธํ ์ด์ ์ ์ถ๊ฐํด์ BeanB๋ ์คํ๋ง ๋น์ ๋ฑ๋ก๋์ง ์๊ธฐ ๋๋ฌธ)
assertThrows(
NoSuchBeanDefinitionException.class,
() -> ac.getBean("beanB", BeanB.class));
}
โ ํ ์คํธ ํต๊ณผ๋ฅผ ์ํด ์ ์ฝ๋ ์์ฑ
3. ํํฐํ์ ์ต์
โ 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
* ์ต๊ทผ ์คํ๋ง ๋ถํธ๋ ์ปดํฌ๋ํธ ์ค์บ์ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋๋ฐ, ์ต์ ์ ๋ณ๊ฒฝํ๋ฉด์ ์ฌ์ฉํ๊ธฐ ๋ณด๋ค๋
์คํ๋ง์ ๊ธฐ๋ณธ ์ค์ ์ ์ต๋ํ ๋ง์ถ์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๊ณ ์ ํธํ๋ค๊ณ ํ์ จ๋ค.