Spring组件的生命周期详细全过程

一、生命周期

Spring容器中的生命周期方法分为4类:

  • Bean自身方法
  • Bean级生命周期接口方法
  • 容器级生命周期接口方法
  • 工厂级生命周期接口方法

1.1 Bean自身生命周期方法

Bean自身的方法:

  • 构造函数实例化bean
  • initMethod指定方法(指定某个方法在bean实例化完成,依赖关系结束后执行)
  • destroyMethod指定方法(指定某个方法在bean销毁之前执行)

定义一个类:

@Component
public class XiaoChao {
   
    @Value("小超")
    private String name;
    @Value("20")
    private int age;


    public XiaoChao() {
   
        System.out.println("实例化bean===>构造器");
    }

    public void xiaochaoInit(){
   
        System.out.println("bean自身方法===>初始化方法");
    }

    public void xiaochaoDestroy() {
   

        System.out.println("bean自身方法====>销毁方法");
    }

    public void hello(){
   
        System.out.println("bean自身方法===>helloWorld");
    }

    public void setName(String name) {
   
        this.name = name;
        System.out.println("bean的属性注入===>"+name);
    }

    public void setAge(int age) {
   
        this.age = age;
        System.out.println("bean的属性注入===>"+age);
    }


}

配置bean

@Configuration
public class MyConfiguration {
   
    
    //initMethod:指定初始化方法
    //destroyMethod:指定销毁方法
    @Bean(initMethod = "xiaochaoInit",destroyMethod = "xiaochaoDestroy")
    public XiaoChao xiaoChao(){
   
        return new XiaoChao();
    }
}

1.2 Bean级生命周期接口方法

Bean级生命周期方法有:

  • InitializingBean接口(bean实例化完成,属性值注入后调用,initMethod之前调用)
public interface InitializingBean {
   

	/**
	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
	 * <p>This method allows the bean instance to perform validation of its overall
	 * configuration and final initialization when all bean properties have been set.
	 * @throws Exception in the event of misconfiguration (such as failure to set an
	 * essential property) or if initialization fails for any other reason
	 */
	void afterPropertiesSet() throws Exception;

}
  • DisposableBean接口(bean销毁前调用,destroyMethod之前调用)
public interface DisposableBean {
   

	/**
	 * Invoked by the containing {@code BeanFactory} on destruction of a bean.
	 * @throws Exception in case of shutdown errors. Exceptions will get logged
	 * but not rethrown to allow other beans to release their resources as well.
	 */
	void destroy() throws Exception;

}
  • BeanNameAware接口(bean实例化时给bean注入beanName)
public interface BeanNameAware extends Aware {
   

	/**
	 * Set the name of the bean in the bean factory that created this bean.
	 * <p>Invoked after population of normal bean properties but before an
	 * init callback such as {@link InitializingBean#afterPropertiesSet()}
	 * or a custom init-method.
	 * @param name the name of the bean in the factory.
	 * Note that this name is the actual bean name used in the factory, which may
	 * differ from the originally specified name: in particular for inner bean
	 * names, the actual bean name might have been made unique through appending
	 * "#..." suffixes. Use the {@link BeanFactoryUtils#originalBeanName(String)}
	 * method to extract the original bean name (without suffix), if desired.
	 */
	void setBeanName(String name);

}

  • ApplicationContextAware接口(bean实例化时给bean注入ApplicationContext)
public interface ApplicationContextAware extends Aware {
   

	/**
	 * Set the ApplicationContext that this object runs in.
	 * Normally this call will be used to initialize the object.
	 * <p>Invoked after population of normal bean properties but before an init callback such
	 * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
	 * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
	 * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
	 * {@link MessageSourceAware}, if applicable.
	 * &
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值