Spring源码(七)--Bean生命周期相关的接口

Bean生命周期相关的接口

Bean生命周期接口相关的这些类和接口,都可以多看看。
Bean工厂实现应该尽可能支持标准的Bean生命周期接口。
整套方法及其标准顺序为:

在 bean 实例化,并设置属性后,就会调用以下的方法。

* 
 * <li>BeanNameAware's {@code setBeanName}
 * <li>BeanClassLoaderAware's {@code setBeanClassLoader}
 * <li>BeanFactoryAware's {@code setBeanFactory}
 * <li>EnvironmentAware's {@code setEnvironment}
 * <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
 * <li>ResourceLoaderAware's {@code setResourceLoader}
 * (only applicable when running in an application context)
 * <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
 * (only applicable when running in an application context)
 * <li>MessageSourceAware's {@code setMessageSource}
 * (only applicable when running in an application context)
 * <li>ApplicationContextAware's {@code setApplicationContext}
 * (only applicable when running in an application context)
 * <li>ServletContextAware's {@code setServletContext}
 * (only applicable when running in a web application context)
 * <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors
 * <li>InitializingBean's {@code afterPropertiesSet}
 * <li>a custom init-method definition
 * <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors

BeanNameAware

/**
 *  该接口由希望在beanFactory中知道其bean名称的bean实现。
 */
public interface BeanNameAware extends Aware {

    /**
     *  在创建此bean的bean工厂中设置bean的名称。
     */
    void setBeanName(String name);

}

BeanClassLoaderAware

/**
 *  允许bean知道bean类加载器的回调;也就是,当前bean工厂用来加载bean类的类加载器。
 */
public interface BeanClassLoaderAware extends Aware {

    /**
     * 将bean类加载器提供给bean实例的回调。
     */
    void setBeanClassLoader(ClassLoader classLoader);

}

BeanFactoryAware

/**
 *   BeanFactoryAware 接口将由希望知道自己拥有的BeanFactory的bean实现。
 * /
public interface BeanFactoryAware extends Aware {

	/**
	 *  向bean实例提供所属工厂的回调。
	 */
	void setBeanFactory(BeanFactory beanFactory) throws BeansException;

}

EnvironmentAware


/**
  *  实现此接口的bean,能够收到其所运行的环境通知。
  */
public interface EnvironmentAware extends Aware {

	/**
	 *  设置该组件运行的环境。
	 */
	void setEnvironment(Environment environment);

}

EmbeddedValueResolverAware

/**
  *   此接口由任何希望收到StringValueResolver(用于解析嵌入定义值)通知的对象实现。
  */
public interface EmbeddedValueResolverAware extends Aware {

	/**
	 *  设置StringValueResolver以用于解析嵌入的定义值。
	 */
	void setEmbeddedValueResolver(StringValueResolver resolver);

}

ResourceLoaderAware


/**
  *  此接口可以由任何对象实现,该对象希望得到它所运行的ResourceLoader(通常是ApplicationContext)的通知。
  * 这是通过ApplicationContextAware接口实现完整ApplicationContext依赖的替代方案。
  */
public interface ResourceLoaderAware extends Aware {

	/**
	 *   设置该对象运行的ResourceLoader。
	 */
	void setResourceLoader(ResourceLoader resourceLoader);

}


ApplicationEventPublisherAware


/**
  *  接口可以由任何对象实现,该对象希望得到它所运行的ApplicationEventPublisher(通常是ApplicationContext)的通知。
  */
public interface ApplicationEventPublisherAware  extends Aware {

	void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);

}

MessageSourceAware


/**
  *  此接口由任何对象实现,该对象希望收到运行在其中的MessageSource(通常是ApplicationContext)的通知。
  */
public interface MessageSourceAware extends Aware {

	void setMessageSource(MessageSource messageSource);

}

ApplicationContextAware


/**
  * 此接口必须由任何对象实现,该对象希望得到其运行的ApplicationContext的通知。
  * 例如,当一个对象需要访问一组协作bean时,实现这个接口是有意义的。
  */
public interface ApplicationContextAware extends Aware {

	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

ServletContextAware


/**
  *    此接口可以由任何对象实现,该对象希望得到其所运行的ServletContext(通常由WebApplicationContext决定)的通知。
  */
public interface ServletContextAware extends Aware {
    void setServletContext(ServletContext var1);
}

BeanPostProcessor 之 postProcessBeforeInitialization

postProcessBeforeInitialization: 初始化之前执行。

BeanPostProcessor常见的使用的场景,是处理标记接口实现类,或者为当前对象提供代理实现。
比如:BeanPostProcessor的 实现类之一 ApplicationContextAwareProcessor ,
详情见: https://blog.csdn.net/sinat_32502451/article/details/140425175


/**
  * 允许自定义修改新bean实例; 例如,检查标记接口或用代理包装bean。
  * 通常,通过标记接口或类似的方式填充bean的后处理器将实现 postProcessBeforeInitialization,
  * 而用代理包装bean的后处理器通常将实现 postProcessAfterInitialization。
  * 注册一个 ApplicationContext可以在它的bean定义中自动检测 BeanPostProcessor bean,并将这些后处理器应用于随后创建的任何bean。
  * 一个普通的 BeanFactory 允许注册后置处理器,将它们应用到通过bean工厂创建的所有bean上。
  */
public interface BeanPostProcessor {

	/**
	 *  在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义初始化方法) 之前,将此BeanPostProcessor应用于给定的新bean实例。
	 * 这个bean已经被属性值填充了。返回的bean实例可能是原始bean实例的包装器。默认实现按原样返回给定的bean。
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}


}

InitializingBean

初始化bean。


/**
  *  该接口将由bean实现,当bean的所有属性都被BeanFactory设置后,bean需要做出反应。
  * 例如,执行自定义初始化,或者仅仅检查所有强制属性是否已经设置。
  */
public interface InitializingBean {

	/**
	 * 在设置了所有bean属性并满足BeanFactoryAware、ApplicationContextAware等要求后,由包含bean的BeanFactory调用。
	 * 此方法允许bean实例在设置了所有bean属性后对其总体配置和最终初始化执行验证。
	 */
	void afterPropertiesSet() throws Exception;

}

init-method

如果Bean在Spring的 xml 配置文件中配置了init-method属性,会自动调用其配置的初始化方法。

BeanPostProcessor 之 postProcessAfterInitialization

初始化之后执行。

public interface BeanPostProcessor {


	/**
	 * 在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义初始化方法)之后,
	 * 将此 BeanPostProcessor 应用于给定的新bean实例。
	 * 这个bean已经被属性值填充了。返回的bean实例可能是原始bean实例的包装器。
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

DisposableBean


/**
  *  此接口将由希望在销毁时释放资源的bean实现。BeanFactory将在单独销毁作用域bean时调用destroy方法。
  * org.springframework.context.ApplicationContext应该在应用程序生命周期驱动下,在关闭时处理它的所有单例。
  */
public interface DisposableBean {

	/**
	 * 由包含bean的BeanFactory在销毁bean时调用。
	 */
	void destroy() throws Exception;

}

posted on 2024-07-26 00:03  乐之者v  阅读(1)  评论(0编辑  收藏  举报

导航