IOC操作Bean管理XML方式(bean 的生命周期)

目录

 

IOC操作Bean管理XML方式(bean 的生命周期)

 1.bean 的生命周期(在单例模式下的生命周期)

(1)通过构造器创建 bean 的实例(执行类中无参构造方法去创建对象)

(2)为 bean 的属性设置值、或者对其他外部 bean 的引用(调用set 方法注入属性)

(3)调用 bean 的初始化方法(需要我们进行配置初始化的方法)

(4)bean 可以使用了(也就是说对象获取到了)

(5)当容器关闭的时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

2.演示bean  的生命周期

步骤1:创建一个bean包,并且创建一个Orders类

步骤2:新建一个bean4.xml

步骤3:测试方法调用以及手写销毁方法调用

步骤4:实验结果

 3.补充:bean 的后置处理器,bean的生命周期有七步

 4.演示添加后置处理器效果

(1)创建一个MyBeanPost 类,实现接口 BeanPostProcessor,创建后置处理器

(2)上面的MyBeanPost类实例出来,但是就是一个普通类,咱们Spring 并不知道这是一个后置处理器,所以我们需要进行配置

(3)分析测试结果


 

IOC操作Bean管理XML方式(bean 的生命周期)

 

什么是生命周期:从一个对象创建到一个对象销毁的过程就是生命周期

bean 的生命周期:创建-DI-初始化-使用-销毁

{}的内容最先输出,其次是构造器,然后是赋值 


 

 1.bean 的生命周期(在单例模式下的生命周期)

(1)通过构造器创建 bean 的实例(执行类中无参构造方法去创建对象)

package com.lbj.spring5.bean;


public class Orders {

    //为了明显,我们写一个无参构造方法
    public Orders(){
        System.out.println("第一步:执行无参构造创建bean实例");
    }

    private String  oname;

    public void setOname(String oname) {
        this.oname = oname;
    }
}

 

(2)为 bean 的属性设置值、或者对其他外部 bean 的引用(调用set 方法注入属性)

xml

java

 

(3)调用 bean 的初始化方法(需要我们进行配置初始化的方法)

java

xml 

 

(4)bean 可以使用了(也就是说对象获取到了)

java

 

(5)当容器关闭的时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

java

xml 

执行到这一步,其实并没有让bean 销毁

需要手动让bean 实例销毁,意思是,如果我此时方法中不主动去调用销毁方法,我的bean.xml配置文件中也不会执行销毁

 

 TestSpring5Demo1的测试代码如下:

package com.lbj.spring5.testdemo;

import com.lbj.spring5.bean.Orders;
import com.lbj.spring5.collectiontype.Book;
import com.lbj.spring5.collectiontype.Course;
import com.lbj.spring5.collectiontype.Student;
import com.lbj.spring5.factorybean.Mybean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5Demo1 {


    @Test
    public void tsetBean1(){

//        ApplicationContext context=
//                new ClassPathXmlApplicationContext("bean4.xml");

        //使用的是ApplicationContext的子类进行调用close()方法,是一种向下转型
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders=context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建 bean 实例对象");
        System.out.println(orders);
//5.手动让bean 实例销毁
        context.close();
    }
}

 

 

2.演示bean  的生命周期

步骤1:创建一个bean包,并且创建一个Orders类

Orders类代码如下:

package com.lbj.spring5.bean;

public class Orders {

    //1.为了明显,我们写一个无参构造方法
    public Orders(){
        System.out.println("第一步:执行无参构造创建bean实例");
    }

    private String  oname;
//2.调用set 方法
    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步 调用 set 方法设置属性值");
    }

    //3.创建执行的初始化方法,然后去bean中配置,让这个普通的初始化方法执行
    public void initMethod(){
        System.out.println("第三步:执行初始化的方法");
    }

//    5.创建销毁的方法
    public void destoryMethod(){
        System.out.println("第五步:执行销毁的方法");
    }
}

步骤2:新建一个bean4.xml

在bean4.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:p="http://www.springframework.org/schema/p"

       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="orders" class="com.lbj.spring5.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
    <property name="oname" value="华为手机"></property>
</bean>
</beans>

 

步骤3:测试方法调用以及手写销毁方法调用

package com.lbj.spring5.testdemo;

import com.lbj.spring5.bean.Orders;
import com.lbj.spring5.collectiontype.Book;
import com.lbj.spring5.collectiontype.Course;
import com.lbj.spring5.collectiontype.Student;
import com.lbj.spring5.factorybean.Mybean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestSpring5Demo1 {


    @Test
    public void tsetBean1(){

//        ApplicationContext context=
//                new ClassPathXmlApplicationContext("bean4.xml");

        //使用的是ApplicationContext的子类进行调用close()方法,是一种向下转型
        ClassPathXmlApplicationContext context=
                new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders=context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建 bean 实例对象");
        System.out.println(orders);
//5.手动让bean 实例销毁
        context.close();
    }
}

 

步骤4:实验结果

 

 3.补充:bean 的后置处理器,bean的生命周期有七步

(1)通过构造器创建 bean 的实例(执行类中无参构造方法去创建对象)

(2)为 bean 的属性设置值、或者对其他外部 bean 的引用(调用set 方法注入属性)

(2.1)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization

(3)调用 bean 的初始化方法(需要我们进行配置初始化的方法)

(3.1)把bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization

(4)bean 可以使用了(也就是说对象获取到了)

(5)当容器关闭的时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

4.演示添加后置处理器效果

(1)创建一个MyBeanPost 类,实现接口 BeanPostProcessor,创建后置处理器

按住ctrl,鼠标点击接口就会进入到接口内查看接口中的抽象方法

进入接口后:

把接口的这两个抽象方法复制出来到MyBeanPost 类

复制到MyBeanPost后,代码如下:

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


public class MyBeanPost implements BeanPostProcessor{
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

修改代码后如下:

package com.lbj.spring5.bean;

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

public class MyBeanPost implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之前执行的方法");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之后执行的方法");
        return bean;
    }
}

 

(2)上面的MyBeanPost类实例出来,但是就是一个普通类,咱们Spring 并不知道这是一个后置处理器,所以我们需要进行配置

bean4.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:p="http://www.springframework.org/schema/p"

       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="orders" class="com.lbj.spring5.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
    <property name="oname" value="华为手机"></property>
</bean>

    <!--配置后置处理器-->
    <!--这么做之后,我们的所有bean 实例都会自动添加上后置处理器-->
    <!--MyBeanPost 类中的方法也会被执行-->
    <bean id="myBeanPost" class="com.lbj.spring5.bean.MyBeanPost">

    </bean>
</beans>

 

(3)分析测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值