bean的生命周期

  • 执行无参构造器,创建了bean实例
  • 调用了set方法,设置了属性值
  • 执行了初始化的方法
  • 获得了创建bean实例对象
  • 执行了销毁的容器的方法
package com.guodaxia.bean.lifttime;

public class TestTime {

    String name;
    public TestTime() {
        System.out.println("执行第一步,无参构造");
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("执行第二步,set方法");
    }

    public void initMethod(){
        System.out.println("执行第三步,初始化的方法");
    }
    //这里采用手动销毁,用于演示生命周期
    public void destroyMethod(){
        System.out.println("执行第五步,销毁的方法");
    }
}
package com.guodaxia.bean.lifttime;


import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        TestTime time = context.getBean("mybean",TestTime.class);
        System.out.println("执行第四步,创建bean对象");
        System.out.println(time);
    }
}
<?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="mybean" class="com.guodaxia.bean.lifttime.TestTime" init-method="initMethod" destroy-method="destroyMethod">
        <property name="name" value="苹果"></property>
    </bean>
</beans>
posted @ 2023-06-06 12:10  郭培鑫同学  阅读(0)  评论(0编辑  收藏  举报