Spring Bean的生命周期

1. 测试代码

//User类
public class User
{
    private String username;

    public User()
    {
        System.out.println("new User() 实例化...");
    }

    public void init()
    {
        System.out.println("User.init() 初始化...");
    }

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

    public String getUsername()
    {
        System.out.println("User.getUsername()");
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
        System.out.println("User.setUsername(" + username + ")");
    }
}

// 测试类
public class Main
{
    @Test
    public void test()
    {
        //1.加载并解析xml文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 2.获取User对象
        User user = (User) context.getBean("user");

        // 3. 使用对象
        user.getUsername();

        // 4. 关闭
        context.close();
    }
}

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

    <!--
    注意:
        1. id值必须唯一,相当于new对象的引用名称
        2. class:包名+类名,按【ctrl+鼠标左键】可定位到对应的class
        3. init-method:指定需要进行初始化的方法名称
        4. destroy-method:指定需要销毁资源的方法名称
     -->
    <bean id="user" class="" init-method="init" destroy-method="destroy">
        <!--
            property是set方法注入依赖,调用了User对象的set方法。
            name: 跟set方法名称一致,首字母小写,set不用写。
            value: set方法的参数。
         -->
        <property name="username" value="孤云"></property>
    </bean>

</beans>

2. 测试结果

在这里插入图片描述

3. set方法为什么比init方法先执行呢?

通过debug对比调用栈发现会先解析xml的属性值然后在初始化。

  • set方法的调用栈。
    在这里插入图片描述

  • init方法的调用栈。
    在这里插入图片描述

  • 在调用doCreateBean时, 2个调用栈发生了变化。先执行populateBean做了一些设置值的操作,执行到BeanWrapperImpl.setValue方法时,里面用了反射调用User类的set方法。
    initializeBean中在执行到invokeCustomInitMethod时用反射调用的init方法。
    在这里插入图片描述

  • invokeCustomInitMethod方法的描述信息。

/**
* Invoke the specified custom init method on the given bean.
* Called by invokeInitMethods.
* <p>Can be overridden in subclasses for custom resolution of init
* methods with arguments.
* @see #invokeInitMethods
*/
protected void invokeCustomInitMethod(String beanName, Object bean, RootBeanDefinition mbd) throws Throwable {

4. 把property注释掉,在测试类中调用set方法。

    @Test
    public void test()
    {
        //1.加载并解析xml文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 2.获取User对象
        User user = (User) context.getBean("user");

        // 3. 使用对象
        user.setUsername("孤云");

        // 4. 关闭
        context.close();
    }

执行结果:

new User() 实例化…
User.init() 初始化…
User.setUsername(孤云)
User.destroy() 销毁…

5. 总结

  • 首先会执行对象构造方法。
  • 其次是配置文件中的<property>的内容,也就是set方法。如果没有配置这个就执行下一步。
  • 接着是执行在配置文件中指定的init-method。
  • 再接着是执行对象里的普通方法(get、set、toString、自定义的方法等)。
  • 最后是指定的destroy-method。

6. 结语

文章内容仅为个人理解,如有错误欢迎指出。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值