Spring Bean 生命周期

 

 

 

 

创建实体类

package lbl.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

import java.io.Serializable;

/**
 * 01.BeanNameAware.就是让student类感觉到自己在容器中id或者是name
 * 02.BeanFactoryAware.就是让student类感觉到自己在容器中所属的bean工厂
 * 03.InitializingBean.就是为了执行初始化之后的操作 ,但是对spring产生了依赖
 *         后续使用反射机制 init-method 来消除对spring的依赖
 * 04.DisposableBean.就是为了执行bean销毁之后的操作 ,但是对spring产生了依赖
 *         后续使用反射机制 destroy-method 来消除对spring的依赖
 */
public class Student implements BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean {
    private int sid;
    private String sname;

    private String BeanName;
    private String BeanFactory;

    public Student() {
        System.out.println("---Student 无参构造---");
    }


    @Override
    public void setBeanName(String s) {
        System.out.println("---Student 中 setBeanName---");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("---Student 中 setBeanFactory---");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("---Student 中 afterPropertiesSet---");
    }
    public void MyafterPropertiesSet() throws Exception {
        System.out.println("---Student 中 MyAfterPropertiesSet---");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("---Student 中 destroy---");
    }

    public void Mydestroy() throws Exception {
        System.out.println("---Student 中 Mydestroy---");
    }
    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                '}';
    }
}

配置Spring.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="student" class="lbl.bean.Student" destroy-method="Mydestroy" init-method="MyafterPropertiesSet">
        <property name="sid" value="20"/>
        <property name="sname" value="小明"/>
    </bean>
</beans>

测试类

import lbl.bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
        Student student= context.getBean("student", Student.class);
        System.out.println(student);
        //context没有close方法需要强转
        ((ClassPathXmlApplicationContext)context).close();


    }
}

显示结果

所以bean简单的生命周期为  创建bean的无参构造-->调用BeanNameAware感觉容器中id-->创建BeanFactory工厂-->进行初始化操作-->输出-->销毁

在实例化过程中spring又有几种进程

InstantiationAwareBeanPostProcessorAdapter类

package com.xdf.bean;

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

import java.beans.PropertyDescriptor;

public class MyInitAwareBeanpostAdpater  extends InstantiationAwareBeanPostProcessorAdapter{

    public  MyInitAwareBeanpostAdpater(){
        System.out.println("*****MyInitAwareBeanpostAdpater的无参构造*****");
    }


    //在实例化bean之前调用
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessBeforeInstantiation *****");
        return null; //底层返回的就是null
    }


    //在实例化bean之后调用
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessAfterInitialization *****");
        return bean;
    }
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessPropertyValues *****");
        return pvs;
    }


}

implements  BeanPostProcessor接口

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Processor  本意是加工 处理的意思!
 *
 * 实现了BeanPostProcessor
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    public MyBeanPostProcessor(){
        System.out.println("===MyBeanPostProcessor的无参构造方法 ===");
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("===执行了BeanPostProcessor中的 postProcess ==Before==Initialization  ===");
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("===执行了BeanPostProcessor中的 postProcess ==After==Initialization  ===");
        return bean;
    }
}

implements  BeanFactoryPostProcessor接口

package lbl.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * Processor 本意是加工 处理的意思
 *
 * 实现BeanFactoryPostProcessor
 * 在实例化后进行修改
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
    public MyBeanFactoryPostProcessor() {
        System.out.println("--- MyBeanFactoryPostProcessor无参构造 ---");
    }
    /*

     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("--- postProcessBeanFactory方法 ---");
        BeanDefinition students = beanFactory.getBeanDefinition("student");//需要写xml中对应Bean中id名称
        students.getPropertyValues().addPropertyValue("sname","小红");
    }
}

写完之后一定要配置spring.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 配置文件-->
    <bean class="lbl.bean.MyBeanFactoryPostProcessor"/>
    <bean class="lbl.bean.MyBeanPostProcessor"/>
    <bean class="lbl.bean.MyInitAwareBeanpostAdpater"/>
    <!-- 配置实体类-->
    <bean id="student" class="lbl.bean.Student" destroy-method="Mydestroy" init-method="MyafterPropertiesSet">
        <property name="sid" value="20"/>
        <property name="sname" value="小明"/>
    </bean>
</beans>

运行结果为

 

posted on 2018-01-04 12:01  站立的猫  阅读(132)  评论(0)    收藏  举报

导航