IOC容器-bean的生命周期

IOC容器-bean的生命周期_第1张图片

初始化和销毁方法需要配置

xml配置文件:
IOC容器-bean的生命周期_第2张图片

<?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: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="book" class="com.spring5.Book" init-method="initMethod" destroy-method="destroyMethod">
        <property name="bname" value="西游记"></property>
    </bean>
</beans>

Book类:

package com.spring5;

public class Book {
    private String bname;

    public Book() {
        System.out.println("第一步,执行无参构造方法创建bean实例");
    }
    public void setBname(String bname) {
        this.bname = bname;
        System.out.println("第二步:调用set方法设置属性值");
    }

    public void initMethod(){
        System.out.println("第三步:执行初始化方法");
    }
    public void  destroyMethod(){
        System.out.println("第五步:执行销毁方法");
    }
}


测试类:

package com.spring5.testdemo;

import com.spring5.Book;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testspring5 {

    @Test
    public void test(){
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("com/bean2.xml");
//        第四步:获取创建bean实例对象
        Book book = context.getBean("book", Book.class);
        System.out.println(book);

        context.close();
    }
}

IOC容器-bean的生命周期_第3张图片
控制台显示生命周期完整过程:
IOC容器-bean的生命周期_第4张图片
加上bean后置处理器,还有多出两步
IOC容器-bean的生命周期_第5张图片
1)创建类,实现BeanPostProcessor接口

package com.spring5;

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


public class MyBookbean 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)修改spring配置文件

<?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">

    <bean id="book" class="com.spring5.Book" init-method="initMethod" destroy-method="destroyMethod">
        <property name="bname" value="西游记"></property>
    </bean>

    <bean id="mybookbean" class="com.spring5.MyBookbean"></bean>
</beans>

IOC容器-bean的生命周期_第6张图片
控制台显示生命周期过程:
IOC容器-bean的生命周期_第7张图片

你可能感兴趣的:(spring,java,spring,开发语言)