美文网首页spring
Spring中Bean的生命周期

Spring中Bean的生命周期

作者: rainbowz | 来源:发表于2019-07-19 16:11 被阅读0次
    20180426085835757.png

    可以简述为以下几步

    1实例化bean对象(通过构造方法或者工厂方法)

    2设置对象属性(setter等)(依赖注入)

    3如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递Bean的ID。(和下面的一条均属于检查Aware接口)

    4如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身

    5将Bean实例传递给Bean的前置处理器的postProcessBeforeInitialization(Object bean, String beanname)方法

    6调用Bean的初始化方法

    7将Bean实例传递给Bean的后置处理器的8postProcessAfterInitialization(Object bean, String beanname)方法使用Bean

    9容器关闭之前,调用Bean的销毁方法
    代码演示
    实体类

    package com.zdw.Bean;
    
    public class Car {
    
        private  String brand;
    
        private  int price;
    
        @Override
        public String toString() {
            return "Car{" +
                    "brand='" + brand + '\'' +
                    ", price=" + price +
                    '}';
        }
    
        public Car() {
            System.out.println("1--car这个bean初始化");
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            System.out.println("2--设置对象属性setName");
            this.brand = brand;
        }
    
        public int getPrice() {
            return price;
        }
        public  void init(){
            System.out.println("3--car bean的初始化");
        }
        public  void destory(){
            System.out.println("5--car bean的 销毁");
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
        public  void play(){
            System.out.println("4bean的使用");
        }
    }
    
    

    bean的后置处理器,分别在Bean的初始化前后对Bean对象提供自己的实例化逻辑

    - 实现BeanPostProcessor接口
        - postProcessBeforeInitialization方法
        - postProcessAfterInitialization方法
    
    package com.zdw.entity;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
    
        //对初始化之后的Bean进行处理
        //参数:bean:即将初始化的bean
        //参数:beanname:bean的名称
        //返回值:返回给用户的那个bean,可以修改bean也可以返回一个新的bean
    
        public Object postProcessBeforeInitialization(Object bean, String beanname) throws BeansException {
            System.out.println("postProcessBeforeInitialization"+"----此时我的名字:"+bean);
            return bean;
        }
    
    
        public Object postProcessAfterInitialization(Object bean, String beanname) throws BeansException {
    
            com.zdw.Bean.Car car =null;
            System.out.println("对初始化之后的Bean进行处理,将Bean的成员变量的值修改了");
            System.out.println(" postProcessAfterInitialization");
            if("car".equals(beanname) || bean instanceof Car){
                car= (com.zdw.Bean.Car) bean;
                car.setBrand("Porsche");
            }
            return car;
        }
    }
    
    
    

    xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
    
           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-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
    
    
    http://www.springframework.org/schema/mvc ">
    
            <context:component-scan base-package="com.zdw.Bean"/>
    
    
    
        <bean id="car" class="com.zdw.Bean.Car" init-method="init" destroy-method="destory">
            <property name="brand" value="宝马"/>
            <property name="price" value="999999"/>
        </bean>
    
        <bean class="com.zdw.entity.MyBeanPostProcessor"></bean>
    
    
    </beans>
    

    测试

    public class Test {
        public static void main(String[] args) {
    
            ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("life.xml");
    
            Car car =ac.getBean("car",Car.class);
            car.play();
            System.out.println(car.toString());
    
             ac.close();
        }
    }
    
    
    image.png

    参考文章:https://blog.csdn.net/w_linux/article/details/80086950
    完整代码:https://gitee.com/zdwbmw/itcatguigu_spring
    https://www.bilibili.com/video/av50225455/?p=85

    相关文章

      网友评论

        本文标题:Spring中Bean的生命周期

        本文链接:https://www.haomeiwen.com/subject/pdxtlctx.html