์คํ๋ง ์ปจํ ์ด๋
์คํ๋ง ์ปจํ ์ด๋๋ ๋น์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํ๋ ์ญํ ์ ์ํํฉ๋๋ค.
์ข ๋ฅ๋ก๋ BeanFactory, ApplicationContext๋ฑ์ด ์์ผ๋ฉฐ, ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ApplicationContext๋ฅผ ์๋ฏธํฉ๋๋ค.
BeanFactory
์คํ๋ง ๋น์ ๊ด๋ฆฌํ๊ณ ์กฐํํ๋ ์ญํ ์ ๋ด๋นํฉ๋๋ค.
getBean()๋ฑ์ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
ApplicationContext
์ผ๋ฐฉ์ ์ผ๋ก ์คํ๋ง ์ปจํ ์ด๋๋ผ ๋ถ๋ฆ ๋๋ค.
ApplicationContext๋ BeanFactory์ ํ์ ์ธํฐํ์ด์ค์ด๋ฉฐ, ์ฌ๋ฌ ๊ตฌํ์ฒด๊ฐ ์กด์ฌํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
๋ฉ์์ง์์ค๋ฅผ ํ์ฉํ ๊ตญ์ ํ ๊ธฐ๋ฅ - ์๋ฅผ ๋ค์ด์ ํ๊ตญ์์ ๋ค์ด์ค๋ฉด ํ๊ตญ์ด๋ก, ์์ด๊ถ์์ ๋ค์ด์ค๋ฉด ์์ด๋ก ์ถ๋ ฅํฉ๋๋ค.
ํ๊ฒฝ๋ณ์ - ๋ก์ปฌ, ๊ฐ๋ฐ, ์ด์๋ฑ์ ๊ตฌ๋ถํด์ ์ฒ๋ฆฌํฉ๋๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์ด๋ฒคํธ - ์ด๋ฒคํธ๋ฅผ ๋ฐํํ๊ณ ๊ตฌ๋ ํ๋ ๋ชจ๋ธ์ ํธ๋ฆฌํ๊ฒ ์ง์ํฉ๋๋ค
ํธ๋ฆฌํ ๋ฆฌ์์ค ์กฐํ - ํ์ผ, ํด๋์คํจ์ค, ์ธ๋ถ ๋ฑ์์ ๋ฆฌ์์ค๋ฅผ ํธ๋ฆฌํ๊ฒ ์กฐํํฉ๋๋ค
์คํ๋ง ์ปจํ ์ด๋์ ๋น
์คํ๋ง ์ปจํ ์ด๋๋ ๋ด๋ถ์ ์ผ๋ก ์คํ๋ง ๋น์ ์ ์ฅํ๋ ์ ์ฅ ๊ณต๊ฐ์ด ์์ผ๋ฉฐ, ๋น์ ์ด๋ฆ์ Key, ๋น ๊ฐ์ฒด๋ฅผ Value๋ก ํ์ฌ Map์ ์ ์ฅํฉ๋๋ค.
์คํ๋ง ๋น์ ๋ฑ๋กํ ๋์๋ ๋น ์ด๋ฆ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ์๋ ์ด๋ฆ(@Bean์ ์ฌ์ฉํ ๊ฒฝ์ฐ), ํน์ ๋งจ ์์ด ์๋ฌธ์์ธ ํด๋์ค ์ด๋ฆ(@Conponent๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ)์ ์ฌ์ฉํฉ๋๋ค.
๋น ์ด๋ฆ์ @Bean์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ @Bean(name = "์ํ๋ ์ด๋ฆ")์ ํตํด, @Conponent์ ๊ฒฝ์ฐ @Conponent(value="์ํ๋ ์ด๋ฆ")์ ํตํด ์ด๋ฆ์ ์ง์ ๋ถ์ฌํ ์๋ ์์ต๋๋ค.
๋น ์ด๋ฆ์ ํญ์ ๋ค๋ฅธ ์ด๋ฆ์ ๋ถ์ฌํด์ผ ํฉ๋๋ค.
๊ฐ์ ์ด๋ฆ์ ๋ถ์ฌํ๋ฉด ๋ค๋ฅธ ๋น์ด ๋ฌด์๋๊ฑฐ๋, ๊ธฐ์กด ๋น์ ๋ฎ์ด๋ฒ๋ฆฌ๊ฑฐ๋ ์ค์ ์ ๋ฐ๋ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
BeanDefinition - ์คํ๋ง ๋น ์ค์ ๋ฉํ ์ ๋ณด
DefaultListableBeanFactory์์ ๋ค์๊ณผ ๊ฐ์ด ๊ด๋ฆฌ๋ฉ๋๋ค.
์คํ๋ง ๋น์ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ๋ฑ๋ก์ด ๊ฐ๋ฅํฉ๋๋ค.(์ด๋ ธํ ์ด์ ์ฌ์ฉ, XML ์ค์ ์ ๋ณด ์ฌ์ฉ ๋ฑ)
์ด๋ฌํ ์ค์ ์ด ๊ฐ๋ฅํ ์ด์ ๋ ์คํ๋ง ๋น์ ๋ง๋ค ๋ BeanDefinition์ด๋ผ๋ ์ถ์ํ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
์คํ๋ง ๋น์ ์์ฑํ๊ธฐ ์ํด์๋ ์ด๋ ํ ์ ๋ณด๋ฅผ ํตํด์๋ผ๋ BeanDefinition์ ๋ง๋ค ์ ์๋ค๋ฉด ๋น์ ์์ฑํ ์ ์์ต๋๋ค.
ํ๋์ @Bean์์๋ ํ๋์ ๋ฉํ ์ ๋ณด๊ฐ ์์ฑ๋๋ฉฐ, ์ด๋ฌํ ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์คํ๋ง ๋น์ ์์ฑํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์์ฑ๋์ด ์์ต๋๋ค.
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
void setParentName(@Nullable String parentName);
String getParentName();
void setBeanClassName(@Nullable String beanClassName);
String getBeanClassName();
void setScope(@Nullable String scope);
String getScope();
void setLazyInit(boolean lazyInit);
boolean isLazyInit();
void setDependsOn(@Nullable String... dependsOn);
String[] getDependsOn();
void setAutowireCandidate(boolean autowireCandidate);
boolean isAutowireCandidate();
void setPrimary(boolean primary);
boolean isPrimary();
void setFactoryBeanName(@Nullable String factoryBeanName);
String getFactoryBeanName();
void setFactoryMethodName(@Nullable String factoryMethodName);
String getFactoryMethodName();
ConstructorArgumentValues getConstructorArgumentValues();
default boolean hasConstructorArgumentValues() {
return !getConstructorArgumentValues().isEmpty();
}
MutablePropertyValues getPropertyValues();
default boolean hasPropertyValues() {
return !getPropertyValues().isEmpty();
}
void setInitMethodName(@Nullable String initMethodName);
String getInitMethodName();
void setDestroyMethodName(@Nullable String destroyMethodName);
String getDestroyMethodName();
void setRole(int role);
int getRole();
void setDescription(@Nullable String description);
String getDescription();
// Read-only attributes
ResolvableType getResolvableType();
boolean isSingleton();
boolean isPrototype();
boolean isAbstract();
String getResourceDescription();
BeanDefinition getOriginatingBeanDefinition();
}
BeanDefinition์ ์ ๋ณด
- BeanClassName: ์์ฑํ ๋น์ ํด๋์ค ๋ช (์๋ฐ ์ค์ ์ฒ๋ผ ํฉํ ๋ฆฌ ์ญํ ์ ๋น์ ์ฌ์ฉํ๋ฉด ์์)
- factoryBeanName: ํฉํ ๋ฆฌ ์ญํ ์ ๋น์ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ด๋ฆ, ์) appConfig
- factoryMethodName: ๋น์ ์์ฑํ ํฉํ ๋ฆฌ ๋ฉ์๋ ์ง์ , ์) memberService
- Scope: ์ฑ๊ธํค(๊ธฐ๋ณธ๊ฐ)
- lazyInit: ์คํ๋ง ์ปจํ ์ด๋๋ฅผ ์์ฑํ ๋ ๋น์ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ, ์ค์ ๋น์ ์ฌ์ฉํ ๋ ๊น์ง ์ต๋ํ ์์ฑ์ ์ง์ฐ์ฒ๋ฆฌ ํ๋์ง ์ฌ๋ถ
- InitMethodName: ๋น์ ์์ฑํ๊ณ , ์์กด๊ด๊ณ๋ฅผ ์ ์ฉํ ๋ค์ ํธ์ถ๋๋ ์ด๊ธฐํ ๋ฉ์๋ ๋ช
- DestroyMethodName: ๋น์ ์๋ช ์ฃผ๊ธฐ๊ฐ ๋๋์ ์ ๊ฑฐํ๊ธฐ ์ง์ ์ ํธ์ถ๋๋ ๋ฉ์๋ ๋ช
- Constructor arguments, Properties: ์์กด๊ด๊ณ ์ฃผ์ ์์ ์ฌ์ฉํฉ๋๋ค. (์๋ฐ ์ค์ ์ฒ๋ผ ํฉํ ๋ฆฌ ์ญํ ์ ๋น์ ์ฌ์ฉํ๋ฉด ์์)
- ..
AnnotationConfigApplicationContext
AnnotationConfigApplicationContext๋ AnnotatedBeanDefinitionReader๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
AnnotatedBeanDefinitionReader๋ฅผ ํตํด BeanDefinition์ ์์ฑํฉ๋๋ค.
Reference
'๐๏ธ Spring > ๊ธฐ๋ณธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] @ComponentScan (0) | 2021.12.11 |
---|---|
[Spring] @Configuration (0) | 2021.12.11 |
[Spring] ์ฑ๊ธํค (feat. ์ฑ๊ธํค์ ๊ตฌํํ๋ ์ฌ๋ฌ ๋ฐฉ์) (2) | 2021.12.11 |
[Spring] IoC์ DI (0) | 2021.12.11 |
[Spring] ์คํ๋ง์ด๋? (0) | 2021.12.11 |