Spring IOC Notes-Obj Creation Process

摘要: Spring IOC 学习笔记-对象创建过程

正文:

对象创建过程

Resource (xml annotation class properties/yml) -> BeanDefinition -> BeanWrapper -> Object

Resource –> BeanDefinition

For Xml

1
2
3
4
5
6
7
8
9
10
<bean id="user" class="com.itliusir.spring.entity.User" >
<property name="id" value="1" />
<property name="name" value="杰克" />
<property name="classes" ref="aClass" />
</bean>

<bean id="aClass" class="com.itliusir.spring.entity.Class">
<property name="id" value="1" />
<property name="className" value="终极一班" />
</bean>
1
2
3
4
5
6
7
8
9
10
@Test
public void testBean() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext
("classpath:bean.xml");
User user = applicationContext.getBean(User.class);
log.info(user.getName());
log.info(user.getClasses().getClassName());
Assert.assertEquals("杰克", user.getName());
Assert.assertEquals("终极一班", user.getClasses().getClassName());
}
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
31
32
33
34
35
36
1. org.springframework.context.support.ClassPathXmlApplicationContext#
ClassPathXmlApplicationContext(java.lang.String)
2. org.springframework.context.support.AbstractApplicationContext#
refresh
3. org.springframework.context.support.AbstractApplicationContext#
obtainFreshBeanFactory
4. org.springframework.context.support.AbstractRefreshableApplicationContext#
refreshBeanFactory
5. org.springframework.context.support.AbstractXmlApplicationContext#
loadBeanDefinitions(org.springframework.beans.factory.support.DefaultListableBeanFactory)
6. org.springframework.context.support.AbstractXmlApplicationContext#
loadBeanDefinitions(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)
7. org.springframework.beans.factory.support.AbstractBeanDefinitionReader#
loadBeanDefinitions(org.springframework.core.io.Resource...)
8. org.springframework.beans.factory.xml.XmlBeanDefinitionReader#
loadBeanDefinitions(org.springframework.core.io.Resource)
9. org.springframework.beans.factory.xml.XmlBeanDefinitionReader#
loadBeanDefinitions(org.springframework.core.io.support.EncodedResource)
10. org.springframework.beans.factory.xml.XmlBeanDefinitionReader#
doLoadBeanDefinitions
11. org.springframework.beans.factory.xml.XmlBeanDefinitionReader#
registerBeanDefinitions
12. org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#
registerBeanDefinitions
13. org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#
doRegisterBeanDefinitions
14. org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#
parseBeanDefinitions
15. org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#
parseDefaultElement
16. org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#
processBeanDefinition
17. org.springframework.beans.factory.support.BeanDefinitionReaderUtils#
registerBeanDefinition
18. org.springframework.beans.factory.support.DefaultListableBeanFactory#
registerBeanDefinition

For Annotation

1
2
3
4
5
6
@Data
@Component
public class SimpleBean {
private String id = "sss";
private String name = "simpleBeans";
}
1
2
3
4
5
6
7
8
9
@Test
public void testBean() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.itliusir.spring.beans.annotation");
SimpleBean simpleBean = applicationContext.getBean(SimpleBean.class);
log.info(simpleBean.getId());
log.info(simpleBean.getName());
Assert.assertEquals("sss", simpleBean.getId());
Assert.assertEquals("simpleBeans", simpleBean.getName());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
1. org.springframework.context.annotation.AnnotationConfigApplicationContext#
AnnotationConfigApplicationContext(java.lang.String...)
2. org.springframework.context.annotation.AnnotationConfigApplicationContext#
scan
3. org.springframework.context.annotation.ClassPathBeanDefinitionScanner#
scan
4. org.springframework.context.annotation.ClassPathBeanDefinitionScanner#
doScan
5. org.springframework.context.annotation.ClassPathBeanDefinitionScanner#
registerBeanDefinition
6. org.springframework.beans.factory.support.BeanDefinitionReaderUtils#
registerBeanDefinition

BeanDefinition –> Object

1
2
3
4
5
6
7
8
9
10
1. org.springframework.beans.factory.BeanFactory#
getBean(java.lang.String)
2. org.springframework.beans.factory.support.AbstractBeanFactory#
doGetBean
3. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
4. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
5. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
doCreateBean

CreateBeanInstance

1
2
3
4
1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
createBeanInstance
2. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
instantiateBean

PopulateBean

1
2
1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
populateBean

InitializeBean

1
2
1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#
initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)