์คํ๋ง ๋น ์ค์ ๋ฉํ ์ ๋ณด - BeanDefinition
์คํ๋ง์ ์ด๋ป๊ฒ ์ด๋ฐ ๋ค์ํ ์ค์ ํ์์ ์ง์ํ๋ ๊ฒ์ผ๊น?
๊ทธ ์ค์ฌ์๋ BeanDefinition ์ด๋ผ๋ ์ถ์ํ๊ฐ ์๋ค.
์ฝ๊ฒ ์ด์ผ๊ธฐํด์ ์ญํ ๊ณผ ๊ตฌํ์ ๊ฐ๋ ์ ์ผ๋ก ๋๋ ๊ฒ์ด๋ค!
XML์ ์ฝ์ด์ BeanDefinition์ ๋ง๋ค๋ฉด ๋๋ค.
์๋ฐ ์ฝ๋๋ฅผ ์ฝ์ด์ BeanDefinition์ ๋ง๋ค๋ฉด ๋๋ค.

์คํ๋ง ์ปจํ
์ด๋๋ ์๋ฐ ์ฝ๋์ธ์ง, XML์ธ์ง ๋ชฐ๋ผ๋ ๋๋ค. ์ค์ง BeanDefinition๋ง ์๋ฉด ๋๋ค.
BeanDefinition ์ ๋น ์ค์ ๋ฉํ์ ๋ณด๋ผ ํ๋ค.
@Bean , <bean> ๋น ๊ฐ๊ฐ ํ๋์ฉ ๋ฉํ ์ ๋ณด๊ฐ ์์ฑ๋๋ค.
์คํ๋ง ์ปจํ ์ด๋๋ ์ด ๋ฉํ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์คํ๋ง ๋น์ ์์ฑํ๋ค.
public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
private final AnnotatedBeanDefinitionReader reader;
AnnotationConfigApplicationContext ๋
AnnotatedBeanDefinitionReader ๋ฅผ ์ฌ์ฉํด์ AppConfig.class ๋ฅผ ์ฝ๊ณ
BeanDefinition ์ ์์ฑํ๋ค.
public class GenericXmlApplicationContext extends GenericApplicationContext {
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
GenericXmlApplicationContext ๋
XmlBeanDefinitionReader ๋ฅผ ์ฌ์ฉํด์ appConfig.xml ์ค์ ์ ๋ณด๋ฅผ ์ฝ๊ณ
BeanDefinition ์ ์์ฑํ๋ค.
์คํ๋ง ์ปจํ ์ด๋๋ ์ด BeanDefinition(๋ฉํ์ ๋ณด)์ ๊ธฐ๋ฐ์ผ๋ก ์คํ๋ง ๋น์ ์์ฑํ๋ค.
BeanDefinition ์ดํด๋ณด๊ธฐ
@Test
@DisplayName("๋น ์ค์ ๋ฉํ์ ๋ณด(BeanDefinition) ํ์ธ ")
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
System.out.println("beanDefinitionName = " + beanDefinitionName +
"beanDefinition = " + beanDefinition);
}
}
}
- getBeanDefinitionNames() : ์คํ๋ง์ ๋ฑ๋ก๋ ๋ชจ๋ ๋น์ ์กฐํํ ์ ์๋ ๋ฉ์๋
- getBean(๋น ์ด๋ฆ) : ๋น ์ด๋ฆ์ผ๋ก ๋น ๊ฐ์ฒด(์ธ์คํด์ค)๋ฅผ ์กฐํํ ์ ์๋ ๋ฉ์๋
- getBeanDefinition : Bean์ ๋ํ ์ ๋ณด(BeanDefinition) ๊บผ๋ด๊ธฐ
์คํ๋ง์ด ๋ด๋ถ์์ ์ฌ์ฉํ๋ ๋น์ getRole() ๋ก ๊ตฌ๋ถํ ์ ์๋ค.
ROLE_APPLICATION : ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ์๊ฐ ์ ์ํ ๋น
ROLE_INFRASTRUCTURE : ์คํ๋ง์ด ๋ด๋ถ์์ ์ฌ์ฉํ๋ ๋น
BeanDefinition ์ ๋ณด
- BeanClassName: ์์ฑํ ๋น์ ํด๋์ค ๋ช (์๋ฐ ์ค์ ์ฒ๋ผ ํฉํ ๋ฆฌ ์ญํ ์ ๋น์ ์ฌ์ฉํ๋ฉด ์์)
- factoryBeanName: ํฉํ ๋ฆฌ ์ญํ ์ ๋น์ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ด๋ฆ, ์) appConfig
- factoryMethodName: ๋น์ ์์ฑํ ํฉํ ๋ฆฌ ๋ฉ์๋ ์ง์ , ์) memberService
- Scope: ์ฑ๊ธํค(๊ธฐ๋ณธ๊ฐ)
-
lazyInit: ์คํ๋ง ์ปจํ ์ด๋๋ฅผ ์์ฑํ ๋ ๋น์ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ, ์ค์ ๋น์ ์ฌ์ฉํ ๋ ๊น์ง ์ต๋ํ ์์ฑ์ ์ง์ฐ์ฒ๋ฆฌ ํ๋์ง ์ฌ๋ถ
-
InitMethodName: ๋น์ ์์ฑํ๊ณ , ์์กด๊ด๊ณ๋ฅผ ์ ์ฉํ ๋ค์ ํธ์ถ๋๋ ์ด๊ธฐํ ๋ฉ์๋ ๋ช
-
DestroyMethodName: ๋น์ ์๋ช ์ฃผ๊ธฐ๊ฐ ๋๋์ ์ ๊ฑฐํ๊ธฐ ์ง์ ์ ํธ์ถ๋๋ ๋ฉ์๋ ๋ช
-
Constructor arguments, Properties: ์์กด๊ด๊ณ ์ฃผ์ ์์ ์ฌ์ฉํ๋ค. (์๋ฐ ์ค์ ์ฒ๋ผ ํฉํ ๋ฆฌ ์ญํ ์ ๋น์ ์ฌ์ฉํ๋ฉด ์์)