Spring IoC容器-02-容器级生命周期

深渊向深渊呼唤

0、核心类图

Spring IoC容器-02-容器级生命周期

1、代码


import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

/**
 * 容器级生命周期
 */
public class ContainerLifecycle extends InstantiationAwareBeanPostProcessorAdapter {
    /**
     * 构造器
     */
    public ContainerLifecycle() {
        System.out.println("① 【容器级别】ContainerLifecycle构造器执行了");
    }

    /**
     * 接口方法和实例化Bean之前调用
     */
    @Override
    public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
        System.out.println("② 【容器级别】postProcessBeforeInstantiation方法执行了,class=" 
         + beanClass);
        return null;
    }

    /**
     * 设置某个属性时调用
     * 已经被标记为 Deprecated
     */
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, 
        PropertyDescriptor[] pds, Object bean, String beanName) {
        
        System.out.println("③ 【容器级别】postProcessPropertyValues方法执行了,beanName=" 
          + bean.getClass());
        return pvs;
    }

    /**
     * 接口方法和实例化Bean之后调用
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("④ 【容器级别】postProcessAfterInitialization方法执行了,beanName=" 
          + bean.getClass());
        return null;
    }
}

2、xml配置 spring-chapter2-containerlifecycle.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--ContainerLifecycle-->
    <bean  
       >
    </bean>

</beans>

3、测试代码

import com.test.lifecycle.beanlifcycle.BeanLifecycle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Bean级生命周期+容器级生命周期测试
 */
public class ContainerLifecycleTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:spring-chapter2-beanlifecycle.xml",
                "classpath:spring-chapter2-containerlifecycle.xml");
        BeanLifecycle beanLifecycle = context.getBean("beanLifecycle",BeanLifecycle.class);
        beanLifecycle.sayHello();
        context.close();
    }
}

4、运行结果

① 【容器级别】ContainerLifecycle构造器执行了
② 【容器级别】postProcessBeforeInstantiation方法执行了,class=class com.test.lifecycle.beanlifcycle.BeanLifecycle
1. 【Bean级别】构造器执行了
③ 【容器级别】postProcessPropertyValues方法执行了,beanName=class com.test.lifecycle.beanlifcycle.BeanLifecycle
2. 【Bean级别】setBeanName方法执行了
3. 【Bean级别】setApplicationContext方法执行了
4. 【Bean级别】afterPropertiesSet方法执行了
5. 【Bean级别】init-method指定的方法执行了
④ 【容器级别】postProcessAfterInitialization方法执行了,beanName=class com.test.lifecycle.beanlifcycle.BeanLifecycle
6. 【Bean级别】sayHello方法执行了
7. 【Bean级别】destroy方法执行了
8. 【Bean级别】destroy-method属性指定的方法执行了

 

栏目