看这就好

Bean的生命周期

通常意义上讲的bean的名称周期,指的是bean从亚博创建到初始化,经过一系列的流程,最终销毁体育app的过程。只不过,在Spring中,bean的生命周期是由Spring容器来管理的。在Spring中,我们可以自己来指定bean的初始化和销毁的方法。当我们指定了bean的初始化和销毁方法时,当容器在bean进行到当前生命周期的阶段时,会自动调用我们自定义的初始化和销毁方法。

如何定义初始化和销毁方法?

我们已经知道了由Spring管理bean的生命周期时,我们可以指定bean的初始化和销毁方法,那具体该如何定义这些初始化和销毁方法呢?接下来,我们就介绍第一种定义初始化和销毁方法的方式: 通过@Bean注解指定初始化和销毁方法。

如果是使用XML文件的方式配置bean的话,可以在标签中指定bean的初始化和销毁方法,如下所示。

<bean id = "person" class="io.mykit.spring.plugins.register.bean.Person" init-method="init" destroy-method="destroy">
    <property name="name" value="binghe"></property>
    <property name="age" value="18"></property>
</bean>

这里,需要注意的是,在我们写的Person类中,需要存在init()方法和destroy()方法。而且Spring中规定,这里的init()方法和destroy()方法必须是无参方法,但可以抛异常。

如果我们使用注解的方式,该如何实现指定bean的初始化和销毁方法呢?接下来,我们就一起来搞定它!!

首先,创建一个名称为Student的类,这个类的实现比较简单,如下所示。

package io.mykit.spring.plugins.register.bean;
/**
 * @author binghe
 * @version 1.0.0
 * @description 测试bean的初始化和销毁方法
 */
public class Student {
    
    public Student(){
        System.out.println("Student类的构造方法");
    }

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

    public void destroy(){
        System.out.println("销毁Student对象");
    }
}

接下来,我们将Student类对象通过注解的方式注册到Spring容器中,具体的做法就是新建一个LifeCircleConfig类作为Spring的配置类,将Student类对象通过LifeCircleConfig类注册到Spring容器中,LifeCircleConfig类的代码如下所示。

package io.mykit.spring.plugins.register.config;

import io.mykit.spring.plugins.register.bean.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author binghe
 * @version 1.0.0
 * @description Bean的生命周期
 */
@Configuration
public class LifeCircleConfig {
    @Bean
    public Student student(){
        return new Student();
    }
}

接下来,我们就新建一个BeanLifeCircleTest类来测试容器中的Student对象,BeanLifeCircleTest类的部分代码如下所示。

package io.mykit.spring.test;

import io.mykit.spring.plugins.register.config.LifeCircleConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author binghe
 * @version 1.0.0
 * @description 测试bean的生命周期
 */
public class BeanLifeCircleTest {

    @Test
    public void testBeanLifeCircle01(){
        //创建IOC容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LifeCircleConfig.class);
        System.out.println("容器创建完成...");
    }
}

在前面的文章中,我们说过:对于单实例bean对象来说,在Spring容器创建完成后,就会对单实例bean进行实例化。那么,我们先来运行下BeanLifeCircleTest类中的testBeanLifeCircle01()方法,输出的结果信息如下所示。

Student类的构造方法
容器创建完成...

可以看到,在Spring容器创建完成时,自动调用单实例bean的构造方法,对单实例bean进行了实例化操作。

总之:对于单实例bean来说,在Spring容器启动的时候创建对象;对于多实例bean来说,在每次获取bean的时候创建对象。

现在,我们在Student类中指定了init()方法和destroy()方法,那么,如何让Spring容器知道Student类中的init()方法是用来执行对象的初始化操作,而destroy()方法是用来执行对象的销毁操作呢?如果是使用XML文件配置的话,我们可以使用如下配置来实现。

<bean id="student" class="io.mykit.spring.plugins.register.bean.Student" init-method="init" destroy-method="destroy"></bean>

如果我们在@Bean注解中该如何实现呢?其实就更简单了,我们来看下@Bean注解的源码,如下所示。

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {

	@AliasFor("name")
	String[] value() default {};

	@AliasFor("value")
	String[] name() default {};

	@Deprecated
	Autowire autowire() default Autowire.NO;

	boolean autowireCandidate() default true;

	String initMethod() default "";

	String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

}

看到@Bean注解的源码,相信小伙伴们会有种豁然开朗的感觉:没错,就是使用@Bean注解的initMethod属性和destroyMethod属性来指定bean的初始化方法和销毁方法。

所以,我们在LifeCircleConfig类中的@Bean注解中指定initMethod属性和destroyMethod属性,如下所示。

@Bean(initMethod = "init", destroyMethod = "destroy")
public Student student(){
    return new Student();
}

此时,我们再来运行BeanLifeCircleTest类中的testBeanLifeCircle01()方法,输出的结果信息如下所示。

Student类的构造方法
初始化Student对象
容器创建完成...

从输出结果可以看出,在Spring容器中,先是调用了Student类的构造方法来创建Student对象,接下来调用了Student对象的init()方法来进行初始化。

那小伙伴们可能会问,运行上面的代码没有打印出bean的销毁方法中的信息啊,那什么时候执行bean的销毁方法呢? 这个问题问的很好, bean的销毁方法是在容器关闭的时候调用的。

接下来,我们在BeanLifeCircleTest类中的testBeanLifeCircle01()方法中,添加关闭容器的代码,如下所示。

@Test
public void testBeanLifeCircle01(){
    //创建IOC容器
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LifeCircleConfig.class);
    System.out.println("容器创建完成...");
    context.close();
}

我们再来运行BeanLifeCircleTest类中的testBeanLifeCircle01()方法,输出的结果信息如下所示。

Student类的构造方法
初始化Student对象
容器创建完成...
销毁Student对象

可以看到,此时输出了对象的销毁方法中的信息,说明执行了对象的销毁方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值