์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- cpp
- Python
- ๋ง์ง
- ๋ฐ์ด๋ธ์ค
- ์งํ
- ๋ถ๋์ฐ
- ๊ธ๋ฆฌ
- ๊ตญ๋ด์ฌํ
- ํธ๋ ์ด๋
- ์ฌํ
- ๋ฐ์ดํฐ๋ถ์
- ํด์ธ์ฌํ
- ํฌ์
- ํ๋ก๊ทธ๋๋ฐ
- ๊ฒฝ์
- ํธ๋ ์ด๋ฉ
- ํ์จ
- ์ธ๊ณ๊ฒฝ์
- C++
- ์ฐจํธ๋ถ์
- ๋ฏธ๊ตญ์ฌํ
- ๊ฒฝ์ ์งํ
- ์ผ์
- ๋นํธ์ฝ์ธ
- ์ด๋๋ฆฌ์
- ์๋ฆฌ์ดํธํ๋
- ์ฃผ์
- ๋งค๋งค์ ๋ต
- ์ํธํํ
- ํ์ด์ฌ
- Today
- Total
์ถ์ ๊ณต์
[Spring] ๊ณ ๊ธ ์ด๋ ธํ ์ด์ ์ ๋ฆฌ: @Conditional, @Import ์๋ฒฝ ๊ฐ์ด๋ ๋ณธ๋ฌธ
[Spring] ๊ณ ๊ธ ์ด๋ ธํ ์ด์ ์ ๋ฆฌ: @Conditional, @Import ์๋ฒฝ ๊ฐ์ด๋
dkrehd 2025. 4. 25. 22:45๐ฆ Spring ๊ณ ๊ธ ์ด๋ ธํ ์ด์ ์ ๋ฆฌ: @Conditional, @Import ์๋ฒฝ ๊ฐ์ด๋ (์์ ํฌํจ)
Spring์ ํ๋ก์ ํธ์ ํ์ฅ์ฑ๊ณผ ์ ์ฐ์ฑ์ ๋์ด๊ธฐ ์ํ ๋ค์ํ ๊ณ ๊ธ ์ด๋ ธํ ์ด์ ์ ์ ๊ณตํฉ๋๋ค. ๊ทธ ์ค ํนํ, ์ํฉ์ ๋ฐ๋ผ ๋น(Bean)์ ๋ฑ๋กํ ์ง ๊ฒฐ์ ํ๋ @Conditional, ์ธ๋ถ ์ค์ ํด๋์ค๋ ๋น์ ์ถ๊ฐ๋ก ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉํ๋ **@Import**๋ ๋งค์ฐ ์ ์ฉํ ๊ธฐ๋ฅ์ ๋๋ค.
์ด ๊ธ์์๋ ์ค์ ์ฝ๋ ์์๋ฅผ ํตํด ๋ ์ด๋ ธํ ์ด์ ์ ํ์คํ ์ ๋ฆฌํ๊ฒ ์ต๋๋ค.
โ @Conditional ์ด๋ ธํ ์ด์ ์ดํดํ๊ธฐ
@Conditional์ ํน์ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋ ๋๋ง ๋น์ ๋ฑ๋กํ๋ ๋ฐ ์ฌ์ฉํ๋ ์ด๋ ธํ ์ด์ ์ ๋๋ค. ์ฃผ๋ก ํ๊ฒฝ๋ณ๋ก ๋น์ ๋ค๋ฅด๊ฒ ๋ฑ๋กํ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค.
ํต์ฌ ์์:
- Condition ์ธํฐํ์ด์ค: ์กฐ๊ฑด์ ์ ์ํ๋ ๋ก์ง์ ๋ด๋ ์ธํฐํ์ด์ค์ ๋๋ค. matches() ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
- matches(ConditionContext context, AnnotatedTypeMetadata metadata) ๋ฉ์๋:
- ์ด ๋ฉ์๋๊ฐ true๋ฅผ ๋ฐํํ๋ฉด ํด๋น @Conditional์ด ๋ถ์ ๋์(ํด๋์ค ๋๋ @Bean ๋ฉ์๋)์ด ํ์ฑํ๋ฉ๋๋ค.
- ConditionContext: ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ํ๊ฒฝ(Environment), ๋น ํฉํ ๋ฆฌ(BeanFactory), ํด๋์ค ๋ก๋(ClassLoader) ๋ฑ์ ์ ๊ทผํ ์ ์๋ ์ปจํ ์คํธ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค.
- AnnotatedTypeMetadata: @Conditional์ด ์ ์ฉ๋ ํด๋์ค๋ ๋ฉ์๋์ ์ด๋ ธํ ์ด์ ๋ฉํ๋ฐ์ดํฐ์ ์ ๊ทผํ ์ ์๊ฒ ํด์ค๋๋ค.
๐ ์์ ์ฝ๋๋ก ์์๋ณด๊ธฐ
Case 1: ํญ์ ์ฐธ์ธ ์กฐ๊ฑด (TrueCondition)
// ํญ์ true๋ฅผ ๋ฐํํ๋ Condition ๊ตฌํ์ฒด
class TrueCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
System.out.println("Checking TrueCondition: Always true");
return true;
}
}
// Engine ์ปดํฌ๋ํธ๋ TrueCondition์ ์ฌ์ฉํ๋ฏ๋ก ํญ์ ๋น์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.
@Component
@Conditional(TrueCondition.class)
class Engine {
// ... (Engine ํด๋์ค ๋ด์ฉ)
}
- ์ค๋ช : TrueCondition์ matches() ๋ฉ์๋์์ ํญ์ true๋ฅผ ๋ฐํํฉ๋๋ค. ๋ฐ๋ผ์ @Conditional(TrueCondition.class)๊ฐ ๋ถ์ Engine ํด๋์ค๋ ์ด๋ค ํ๊ฒฝ์์๋ ํญ์ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.
Case 2: ํ๊ฒฝ ์์ฑ ๊ธฐ๋ฐ ์กฐ๊ฑด (OsCondition)
// ์์คํ
ํ๊ฒฝ ์์ฑ 'mode'๊ฐ 'dev'์ผ ๋๋ง true๋ฅผ ๋ฐํํ๋ Condition ๊ตฌํ์ฒด
class OsCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
String mode = env.getProperty("mode");
System.out.println("Checking OsCondition for mode: " + mode);
// 'mode' ์์ฑ์ด 'dev'์ ์ผ์นํ๋์ง ํ์ธ
return "dev".equals(mode);
}
}
// Door ์ปดํฌ๋ํธ๋ OsCondition์ ์ฌ์ฉํ๋ฏ๋ก 'mode=dev'์ผ ๋๋ง ๋น์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.
@Component
@Conditional(OsCondition.class)
class Door {
// ... (Door ํด๋์ค ๋ด์ฉ)
}
- ์ค๋ช : OsCondition์ ConditionContext๋ฅผ ํตํด Environment ๊ฐ์ฒด๋ฅผ ์ป์ด์ mode๋ผ๋ ํ๊ฒฝ ์์ฑ ๊ฐ์ ํ์ธํฉ๋๋ค. ๋ง์ฝ mode ์์ฑ ๊ฐ์ด "dev"๋ผ๋ฉด true๋ฅผ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด false๋ฅผ ๋ฐํํฉ๋๋ค.
- ๊ฒฐ๊ณผ: Door ํด๋์ค๋ ์ ํ๋ฆฌ์ผ์ด์ ์คํ ์ -Dmode=dev ์ ๊ฐ์ JVM ์ต์ ์ด๋, ํ๊ฒฝ ๋ณ์, application.properties ํ์ผ ๋ฑ์ ํตํด mode ์์ฑ์ด "dev"๋ก ์ค์ ๋ ๊ฒฝ์ฐ์๋ง ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค. ๋ค๋ฅธ ๊ฐ(prod ๋ฑ)์ด๊ฑฐ๋ ์ค์ ๋์ง ์์๋ค๋ฉด Door ๋น์ ์์ฑ๋์ง ์์ต๋๋ค.
- ํ์ฉ: ์ด์ฒ๋ผ @Conditional์ ์ฌ์ฉํ๋ฉด ๊ฐ๋ฐ ํ๊ฒฝ, ์ด์ ํ๊ฒฝ ๋ฑ ํน์ ํ๊ฒฝ์ ๋ง๋ ๋น์ ์ ํ์ ์ผ๋ก ๋ฑ๋กํ๊ฑฐ๋, ํน์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์กด์ฌ ์ฌ๋ถ์ ๋ฐ๋ผ ๋น ๋ฑ๋ก์ ๊ฒฐ์ ํ๋ ๋ฑ ๋งค์ฐ ์ ์ฐํ ์ค์ ์ด ๊ฐ๋ฅํด์ง๋๋ค. (์: ํน์ OS์์๋ง ๋์ํ๋ ๋น, Feature Toggle ๋ฑ)
* JVM ์ต์ ๋ณ๊ฒฝ
โ @Import ์ด๋ ธํ ์ด์ ์ดํดํ๊ธฐ
@Import ์ด๋ ธํ ์ด์ ์ ํ๋์ ์ค์ ํด๋์ค์์ ๋ค๋ฅธ ์ค์ ํด๋์ค๋ ๋น์ ๋ถ๋ฌ์ฌ ๋ ์ฌ์ฉํฉ๋๋ค. ํนํ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋์ ์ผ๋ก ์ถ๊ฐ ์ค์ ์ ๊ฐ์ ธ์ฌ ๋ ์ ์ฉํฉ๋๋ค.
์ฃผ์ ์ฌ์ฉ๋ฒ:
- ๋ค๋ฅธ @Configuration ํด๋์ค ์ง์ ์ํฌํธ: ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ์ ๋๋ค.
- ImportSelector ๊ตฌํ์ฒด ์ํฌํธ: ๋์ ์ผ๋ก ์ํฌํธํ ์ค์ ํด๋์ค๋ฅผ ์ ํํด์ผ ํ ๋ ์ฌ์ฉํฉ๋๋ค.
- ImportBeanDefinitionRegistrar ๊ตฌํ์ฒด ์ํฌํธ: ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ๋น ์ ์๋ฅผ ์ง์ ๋ฑ๋กํด์ผ ํ ๋ ์ฌ์ฉํฉ๋๋ค. (๊ณ ๊ธ ๊ธฐ๋ฅ)
- (์ถ๊ฐ) ์ผ๋ฐ ํด๋์ค ์ํฌํธ: @Component ๋ฑ์ด ๋ถ์ง ์์ ์ผ๋ฐ ํด๋์ค๋ฅผ ์ง์ ํ๋ฉด, ํด๋น ํด๋์ค๊ฐ ๋น์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.
๐ ์์ ์ฝ๋๋ก ์์๋ณด๊ธฐ
์ฌ์ฉ๋ฒ 1: ๋ค๋ฅธ @Configuration ํด๋์ค ์ง์ ์ํฌํธ
๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ @Import ์ด๋ ธํ ์ด์ ์ ์ํฌํธํ๊ณ ์ถ์ ์ค์ ํด๋์ค๋ค์ ์ง์ ๋ช ์ํ๋ ๊ฒ์ ๋๋ค.
// ์์: MainConfig์ ์ง์ Config1๊ณผ Config2๋ฅผ ์ํฌํธํ๋ ๊ฒฝ์ฐ
@Configuration
@Import({Config1.class, Config2.class}) // Config1๊ณผ Config2๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์ด
class MainConfig {
@Bean Car car() { return new Car(); }
}
class Config1 { @Bean Car sportsCar() { return new SportsCar(); }}
class Config2 { @Bean Car sportsCar() { return new SportsCar2(); }}
- ๋์ ๋ฐฉ์: ์์ ๊ฐ์ด MainConfig์ @Import({Config1.class, Config2.class})๋ฅผ ์ฌ์ฉํ๋ฉด, Spring์ MainConfig๋ฅผ ๋ก๋ํ ๋ Config1๊ณผ Config2์ ์ ์๋ ๋น๋ค๋ ํจ๊ป ๋ก๋ํฉ๋๋ค.
- ์ฃผ์์ : ์ด ๋ฐฉ์์ ์ง์ ๋ ๋ชจ๋ ์ค์ ์ ๋ฌด์กฐ๊ฑด ๊ฐ์ ธ์ต๋๋ค. ๋ง์ฝ ์ ์์์ฒ๋ผ Config1๊ณผ Config2 ๋ชจ๋ "sportsCar"๋ผ๋ ์ด๋ฆ์ ๋น์ ์ ์ํ๊ณ ์๋ค๋ฉด ์ด๋ป๊ฒ ๋ ๊น์? Spring์ ๋์ผํ ์ด๋ฆ์ ๋น์ด ์ฌ๋ฌ ๊ฐ ์ ์๋ ๊ฒ์ ๊ฐ์งํ๊ณ , ์ด๋ค ๋น์ ์ฌ์ฉํด์ผ ํ ์ง ๊ฒฐ์ ํ ์ ์์ด ์ ํ๋ฆฌ์ผ์ด์ ์์ ์์ ์ NoUniqueBeanDefinitionException ๊ฐ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค. (๋ฌผ๋ก @Primary ์ด๋ ธํ ์ด์ ๋ฑ์ผ๋ก ์ฐ์ ์์๋ฅผ ์ง์ ํ ์๋ ์์ง๋ง, ์๋ํ์ง ์์ ์ถฉ๋์ด ๋ฐ์ํ๊ธฐ ์ฝ์ต๋๋ค.)
- ์ ํฉํ ๊ฒฝ์ฐ: ์ํฌํธํ๋ ค๋ ์ค์ ๋ค์ด ์๋ก ๋ค๋ฅธ ์ด๋ฆ์ ๋น๋ค์ ์ ์ํ๊ณ ์๊ฑฐ๋, ๋ช ํํ ์๋๋ฅผ ๊ฐ์ง๊ณ ๋น ์ ์๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๋ ค๋ ๊ฒฝ์ฐ์ ์ ํฉํฉ๋๋ค. ํ์ง๋ง ์กฐ๊ฑด์ ๋ฐ๋ผ ํน์ ์ค์ ๋ง ์ ํ์ ์ผ๋ก ๋ก๋ํ๊ณ ์ถ๋ค๋ฉด ์ด ๋ฐฉ์์ ์ ํฉํ์ง ์์ต๋๋ค. ์ด๋ ImportSelector๊ฐ ํ์ํฉ๋๋ค.
์ฌ์ฉ๋ฒ 2: ImportSelector๋ก ๋์ ์ ํํ๊ธฐ
์กฐ๊ฑด์ ๋ฐ๋ผ ์ํฌํธํ ์ค์ ์ ๋์ ์ผ๋ก ๊ฒฐ์ ํด์ผ ํ ๋ ImportSelector ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค. selectImports() ๋ฉ์๋์์ ๋ก๋ํ ์ค์ ํด๋์ค์ ์ด๋ฆ ๋ฐฐ์ด์ ๋ฐํํ์ฌ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
์์ ์ฝ๋ ๋ค์ ์ดํด๋ณด๊ธฐ (ImportSelector ํ์ฉ)
// ์ค์ ํด๋์ค 1: SportsCar ๋น ์ ์
class Config1 { @Bean Car sportsCar() { return new SportsCar(); }}
// ์ค์ ํด๋์ค 2: SportsCar2 ๋น ์ ์
class Config2 { @Bean Car sportsCar() { return new SportsCar2(); }}
// ImportSelector ๊ตฌํ์ฒด: @EnableMyAutoConfiguration์ value ์์ฑ ๊ฐ์ ๋ฐ๋ผ Config1 ๋๋ Config2๋ฅผ ์ ํ
class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
AnnotationAttributes attr = AnnotationAttributes.fromMap(
importingClassMetadata.getAnnotationAttributes(EnableMyAutoConfiguration.class.getName()));
String mode = attr.getString("value");
System.out.println("MyImportSelector mode: " + mode);
if ("test".equals(mode)) {
return new String[] {Config1.class.getName()};
} else {
return new String[] {Config2.class.getName()};
}
}
}
// ์ปค์คํ
์ด๋
ธํ
์ด์
: MyImportSelector๋ฅผ ์ํฌํธํ๋๋ก ์ค์
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MyImportSelector.class)
@interface EnableMyAutoConfiguration {
String value() default "";
}
// ๋ฉ์ธ ์ค์ ํด๋์ค: @EnableMyAutoConfiguration ์ฌ์ฉ
@EnableMyAutoConfiguration("test2")
class MainConfig {
@Bean Car car() { return new Car(); }
}
// ... (Main ํด๋์ค ์คํ ๋ถ๋ถ์ ๋์ผ) ...
- ๋์ ์ค๋ช : (์ด์ ์ค๋ช ๊ณผ ๋์ผ) MainConfig์ @EnableMyAutoConfiguration("test2") ๋๋ฌธ์ MyImportSelector๊ฐ ๋์ํ๊ณ , value ๊ฐ "test2"๋ฅผ ์ฝ์ด Config2๋ฅผ ์ํฌํธํฉ๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก "sportsCar" ๋น์ SportsCar2 ํ์ ์ด ๋ฉ๋๋ค.
์ @EnableMyAutoConfiguration ๊ฐ์ ์ปค์คํ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ ๊น์?
์ฌ๊ธฐ์ ํ ๊ฐ์ง ์๋ฌธ์ด ์๊ธธ ์ ์์ต๋๋ค. "๊ตณ์ด @EnableMyAutoConfiguration ์ด๋ ธํ ์ด์ ์ ๋ง๋ค ํ์ ์์ด, MainConfig์ ์ง์ @Import(MyImportSelector.class)๋ฅผ ์ฌ์ฉํ๋ฉด ์ ๋๋์?"
๋ต๋ณ: ๊ธฐ์ ์ ์ผ๋ก๋ ๊ฐ๋ฅํ์ง๋ง, @EnableMyAutoConfiguration ๊ฐ์ ์ปค์คํ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ ๋ฐ์๋ ๋ช ๊ฐ์ง ์ค์ํ ์ด์ ๊ฐ ์์ต๋๋ค.
- ์ถ์ํ ๋ฐ ๊ฐ๋ ์ฑ ํฅ์: @EnableMyAutoConfiguration("test2")๋ "ํน์ ๋ชจ๋('test2')์ ๋ง๋ ์๋ ์ค์ ์ ํ์ฑํํ๋ค"๋ ์๋๋ฅผ ๋ช ํํ๊ฒ ๋๋ฌ๋ ๋๋ค. ๋ฐ๋ฉด @Import(MyImportSelector.class)๋ ๋จ์ํ '์ด๋ค Selector๋ฅผ ์ฌ์ฉํ๋ค'๋ ๊ตฌํ ๋ฐฉ์์ ๋ณด์ฌ์ค ๋ฟ์ ๋๋ค. ์ฌ์ฉ์๋ MyImportSelector์ ๋ด๋ถ ๋์์ ๋ชฐ๋ผ๋ @EnableMyAutoConfiguration๋ง ๋ณด๊ณ ์ด๋ค ๊ธฐ๋ฅ์ด ํ์ฑํ๋๋์ง ์ฝ๊ฒ ์ดํดํ ์ ์์ต๋๋ค. ์ด๋ Spring Boot์ ์๋ง์ @Enable... ์ด๋ ธํ ์ด์ ๋ค์ด ๋ฐ๋ฅด๋ ๋ฐฉ์์ด๊ธฐ๋ ํฉ๋๋ค.
- ํ๋ผ๋ฏธํฐ ์ ๋ฌ ์ฉ์ด์ฑ: MyImportSelector๋ "mode" ๊ฐ("test" ๋๋ "test2")์ ๋ฐ๋ผ ๋ค๋ฅธ ์ค์ ์ ์ํฌํธํด์ผ ํฉ๋๋ค. @EnableMyAutoConfiguration ์ด๋ ธํ ์ด์ ์ value() ์์ฑ์ ์ ์ํจ์ผ๋ก์จ, ์ฌ์ฉํ๋ ์ชฝ(MainConfig)์์ @EnableMyAutoConfiguration("test2")์ฒ๋ผ ๋ช ํํ๊ณ ๊ฐ๊ฒฐํ๊ฒ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค. ๋ง์ฝ @Import(MyImportSelector.class)๋ฅผ ์ง์ ์ฌ์ฉํ๋ค๋ฉด, ์ด "mode" ๊ฐ์ MyImportSelector์ ์ ๋ฌํ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฐพ์์ผ ํฉ๋๋ค (์: ๋ณ๋์ ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐ๋ก ๋ถ์ด๊ฑฐ๋, ํ๊ฒฝ ๋ณ์๋ฅผ ํตํ๋ ๋ฑ). ์ปค์คํ ์ด๋ ธํ ์ด์ ์ ์์ฑ์ ์ฌ์ฉํ๋ ๊ฒ์ด ํจ์ฌ ์ง๊ด์ ์ด๊ณ ๊น๋ํฉ๋๋ค.
- ์ฌ์ฌ์ฉ์ฑ ๋ฐ ์บก์ํ: @EnableMyAutoConfiguration์ MyImportSelector๋ผ๋ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ์ฌ์ฉ์๋ก๋ถํฐ ์จ๊น๋๋ค(์บก์ํ). ๋์ค์ ์ ํ ๋ก์ง์ด ๋ฐ๋์ด MyImportSelector2๋ฅผ ์ฌ์ฉํด์ผ ํ๋๋ผ๋, @EnableMyAutoConfiguration์ ๋ด๋ถ @Import ๋์๋ง ๋ณ๊ฒฝํ๋ฉด ์ฌ์ฉ์๋ ๊ธฐ์กด์ @EnableMyAutoConfiguration("test2") ์ฝ๋๋ฅผ ์์ ํ ํ์๊ฐ ์์ต๋๋ค. ๋ํ, ์ด @Enable... ์ด๋ ธํ ์ด์ ์ ๋ค๋ฅธ ์ฌ๋ฌ ์ค์ ํด๋์ค์์ ์ฌ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
- ๊ด๋ก ์ค์: Spring ์ปค๋ฎค๋ํฐ์์๋ ํน์ ๊ธฐ๋ฅ์ด๋ ์ค์ ์ ํ์ฑํํ ๋ @Enable... ํํ์ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ธ ๊ด๋ก์ ๋๋ค. ์ด ๊ด๋ก๋ฅผ ๋ฐ๋ฅด๋ฉด ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค์ด ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ์ฌ์์ง๋๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก, @EnableMyAutoConfiguration๊ณผ ๊ฐ์ ์ปค์คํ ์ด๋ ธํ ์ด์ ์ ImportSelector๋ฅผ ์ฌ์ฉํ๋ ๋ฐ ํ์๋ ์๋์ง๋ง, ์ฝ๋์ ๊ฐ๋ ์ฑ, ์ถ์ํ ์์ค, ์ฌ์ฌ์ฉ์ฑ, ํ๋ผ๋ฏธํฐ ์ ๋ฌ์ ํธ์์ฑ์ ํฌ๊ฒ ๋์ฌ์ฃผ๊ธฐ ๋๋ฌธ์ ์ ๊ทน ๊ถ์ฅ๋๋ ๋ฐฉ์์ ๋๋ค. ๊ตฌํ ์ธ๋ถ์ฌํญ์ ์จ๊ธฐ๊ณ ๋ช ํํ ์๋๋ฅผ ๋๋ฌ๋ด๋ ์ข์ ์ค๊ณ ํจํด์ ๋๋ค.
๐ฏ ์ข ํฉ ์ ๋ฆฌ: ์ ํ์ํ๊ณ ์ธ์ ์ฌ์ฉํ ๊น์?
@Conditional | ์กฐ๊ฑด๋ถ ๋น ๋ฑ๋ก | ํ๊ฒฝ(๊ฐ๋ฐ, ์ด์, ํ ์คํธ)์ ๋ฐ๋ผ ๋ค๋ฅธ ๋น ๋ฑ๋ก |
@Import | ์ธ๋ถ ์ค์ ํด๋์ค ๊ฐ์ ธ์ค๊ธฐ | ํ๊ฒฝ์ ๋ฐ๋ผ ์ค์ ํ์ผ์ด๋ ๋น ๋ฑ๋ก ๋ณ๊ฒฝ |
@Conditional๊ณผ @Import๋ Spring์์ ์ ๊ณตํ๋ ๊ฐ์ฅ ๊ฐ๋ ฅํ ์ด๋
ธํ
์ด์
์ค ํ๋๋ก, ๋ณต์กํ ์ ํ๋ฆฌ์ผ์ด์
ํ๊ฒฝ์์ ์ ์ฐํ๊ณ ๊ด๋ฆฌ๊ฐ ์ฌ์ด ๋น ๋ฑ๋ก ๋ฐฉ์์ ์ ๊ณตํฉ๋๋ค.
์ด๋ฅผ ์ ์ดํดํ๊ณ ํ์ฉํ๋ฉด ์ฝ๋ ํ์ง๊ณผ ์ ์ง๋ณด์์ฑ์ ํฌ๊ฒ ๋์ผ ์ ์์ต๋๋ค.