AbstractXmlApplicationContext.class -> AbstractApplicationContext.class -> DefaultResourceLoader.class
1.容器初始化,主要完成了ioc容器建立beanDefinition数据映射。并没有看到ioc容器对bean依赖关系进行注入。
AbstractApplicationContext.refresh() ->
.obtainFreshBeanFactory() -> AbstractRefreshableApplicationContext.refreshBeanFactory() ->
AbstractXmlApplicationContext.loadBeanDefinitions(beanFactory) ->
.loadBeanDefinitions(beanDefinitionReader) ->
XmlBeanDefinitionReader.loadBeanDefinitions(configResources)
2.IOC容器依赖注入
AbstractBeanFactory.doGetBean(name,...) ->
AbstractAutowireCapableBeanFactory.createBean(beanName)
3.Spring生命周期
00–>MyLifeCycleBean 构造函数
----initializeBean -> invokeAwareMethods【setBeanName、setBeanClassLoader、setBeanFactory】-----
01–>BeanNameAware接口被调用了, 获取到的beanName:xmlmyLifeCycleBean
02–>BeanFactoryAware接口被调用了
----initializeBean -> invokeAwareInterfaces【setEnvironment、setEmbeddedValueResolver、setResourceLoader、setApplicationEventPublisher、setMessageSource、setApplicationContext】-----
03–>ApplicationContextAware接口被调用了
----initializeBean -> postProcessBeforeInitialization-----
04–>调用postProcessBeforeInitialization方法, 获取到的beanName: xmlMyLifeCycleBean
----initializeBean -> afterPropertiesSet -----
05–>InitializingBean接口被调用了
06–>myInit方法被调用了
----initializeBean -> postProcessAfterInitialization-----
07–>调用postProcessAfterInitialization, 获取到的beanName: xmlmyLifeCycleBean
08–>bean可以被使用了, beanInfo: MyLifeCycleBean{name=‘张三’, age=25}
MyLifeCycleBeanTest.class
package linag.test.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyLifeCycleBeanTest {
@Before("")
public void before() {
System.out.println("---bean生命周期开始---\n");
}
@After("")
public void after() {
System.out.println("\n---bean生命周期结束---");
}
/**
* 生命周期测试
*/
@Test
public void testDefaultConstructor() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
MyLifeCycleBean myLifeCycleBean = applicationContext.getBean(MyLifeCycleBean.class);
System.out.println("08-->bean可以被使用了, beanInfo: " + myLifeCycleBean.toString());
((ClassPathXmlApplicationContext) applicationContext).destroy();
}
}
MyBeanPostProcessor.class
package linag.test.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(!(bean instanceof MyLifeCycleBean)) {
return bean;
}
System.out.println("04-->调用postProcessBeforeInitialization方法, 获取到的beanName: " + beanName);
((MyLifeCycleBean) bean).setName("李四");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(!(bean instanceof MyLifeCycleBean)) {
return bean;
}
System.out.println("07-->调用postProcessAfterInitialization, 获取到的beanName: " + beanName);
((MyLifeCycleBean) bean).setAge(30);
return bean;
}
}
MyLifeCycleBean.class
package linag.test.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
public class MyLifeCycleBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean,
DisposableBean {
// 姓名
private String name;
// 年龄
private int age;
public MyLifeCycleBean() {
System.out.println("00-->MyLifeCycleBean 构造函数");
}
@Override
public void setBeanName(String name) {
System.out.println("01-->BeanNameAware接口被调用了, 获取到的beanName:" + name);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("02-->BeanFactoryAware接口被调用了");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("03-->ApplicationContextAware接口被调用了");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("05-->InitializingBean接口被调用了");
}
public void myInit() {
System.out.println("06-->myInit方法被调用了");
}
@Override
public void destroy() throws Exception {
System.out.println("09-->DisposableBean接口被调用了");
}
public void myDestroy() {
System.out.println("10-->自定义destroy-method方法被调动了");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "MyLifeCycleBean{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
applicationContext.xml
<bean name="xmlmyLifeCycleBean" class="linag.test.spring.MyLifeCycleBean" destroy-method="myDestroy"
init-method="myInit">
<property name="name" value="张三"/>
<property name="age" value="25"/>
</bean>