Spring IoC容器-02-容器級生命週期

0、核心類圖java

一、代碼spring

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;
    }
}

二、xml配置 spring-chapter2-containerlifecycle.xmlide

<?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 id="containerLifecycle" 
       class="com.test.lifecycle.containerlifecycle.ContainerLifecycle">
    </bean>

</beans>

三、測試代碼post

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();
    }
}

四、運行結果測試

① 【容器級別】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屬性指定的方法執行了
相關文章
相關標籤/搜索