victory的博客

长安一片月,万户捣衣声

0%

spring | bean的生命周期

bean的生命周期、bean的后置处理器

1.项目目录

2.bean的生命周期(******)
① 通过构造器或工厂方法创建bean实例
② 为bean的属性设置值和对其他bean的引用
③ 调用bean的初始化方法
④ bean可以使用了
⑤ 当容器关闭时,调用bean的销毁方法

3.Person.java

package com.atguigu.ioc.life;

public class Person {
    private Integer id;
    private String sex;
    private String name;
    
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        System.out.println("Two:依赖注入");
        this.id = id;
    }
    
    public String getSex() {
        return sex;
    }
    
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Person() {
        System.out.println("One:创建对象");
    }
    
    public void init(){
        System.out.println("Three:初始化");
    }
    
    

    @Override
    public String toString() {
        return "Four: Person [id=" + id + ", sex=" + sex + ", name=" + name + "]";
    }

    public void destroy(){
        System.out.println("Five:销毁");
    }
}

4.AfterHandler.java(后置处理器)

package com.atguigu.ioc.life;

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

public class AfterHandler implements BeanPostProcessor{

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Person person = (Person)bean;
        if(person.getSex().equals("男")){
            person.setName("张无忌");
        }else{
            person.setName("赵敏");
        }
        return person;
    }
 
}

5.life.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 测试bean的生命周期 -->
    <bean id="person" class="com.atguigu.ioc.life.Person" init-method="init" destroy-method="destroy">
        <property name="id" value="1001"></property>
        <property name="sex" value="男"></property>
    </bean>
    
    <!-- 测试后置处理器 -->
    <bean class="com.atguigu.ioc.life.AfterHandler"></bean>
</beans>

6.Test.java

package com.atguigu.ioc.life;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("life.xml");
        
        Person person = ac.getBean("person", Person.class);
        System.out.println(person);
        
        ac.close();
    }
}

7.运行结果

One:创建对象
Two:依赖注入
Three:初始化
Four: Person [id=1001, sex=男, name=张无忌]
二月 15, 2022 4:34:28 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2dda6444: startup date [Tue Feb 15 16:34:27 CST 2022]; root of context hierarchy
Five:销毁