Spring中的重要接口

1. BeanFactoryPostProcessor接口

官网地址:https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-factory-extension-factory-postprocessors

org.springframework.beans.factory.config.BeanFactoryPostProcessor. The semantics of this interface are similar to those of the BeanPostProcessor, with one major difference: BeanFactoryPostProcessor operates on the bean configuration metadata. That is, the Spring IoC container lets a BeanFactoryPostProcessor read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessor instances.

简单来说就是BeanFactoryPostProcessor类似于BeanPostProcessor,但是他有很大的不同:BeanFactoryPostProcessor是Bean工厂级别的修改,他主要是在Bean实例化之前操作Bean元数据(BeanDefinition),达到修改最后实例化的Bean特征的效果;而BeanPostProcessor是在Bean实例化之后针对Bean级别的操作。虽然在BeanFactoryPostProcessor中也可以取到Bean实例(如BeanFactory.getBean())然后修改或者设置值等,但这样导致过早实例化违反了标准的Bean生命周期,Spring不建议这样做。

public interface BeanFactoryPostProcessor {

	/**
	 * Bean加载完成但没有实例化前回调,可以在这里添加一些属性方便bean实例化使用
	 */
	void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

2. BeanDefinitionRegistryPostProcess接口

BeanDefinitionRegistryPostProcess是BeanFactoryPostProcessor的子接口,因此他的调用时机和BeanFactoryPostProcessor一样,他扩展了一个新的接口方法:

public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

	/**
	 * Bean加载完成但没有实例化前回调,可以在这里添加新的BeanDefinition,容器会一起初始化。
	 */
	void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}

看源码调用过程是先执行org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry(BeanDefinitionRegistry),后执行org.springframework.beans.factory.config.BeanFactoryPostProcessor.postProcessBeanFactory(ConfigurableListableBeanFactory)方法:

org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory, List)部分源码

public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();

		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					registryProcessors.add(registryProcessor);
				}
				else {
					regularPostProcessors.add(postProcessor);
				}
			}
		invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

	}
}

3. BeanPostProcessor接口

官网地址:https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-factory-extension-bpp

The BeanPostProcessor interface defines callback methods that you can implement to provide your own (or override the container’s default) instantiation logic, dependency resolution logic, and so forth. If you want to implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean, you can plug in one or more custom BeanPostProcessor implementations.

简言之BeanPostProcessor是Bean实例化之后的处理器,接口本省只提供了两个方法,他们的执行时机如下源码:

public interface BeanPostProcessor {

	/**
	 * Bean实例化但初始化前执行
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 *Bean实例化并且初始化后执行
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

Spring还提供了BeanPostProcessor的一些子接口如:MergedBeanDefinitionPostProcessor、InstantiationAwareBeanPostProcessor、SmartInstantiationAwareBeanPostProcessor 。这些子接口分别扩展了一些接口方法,他们的执行顺序及作用请查看前一篇博文完整Spring Bean的生命周期源码解析

4. ImportSelector接口

ImportSelector可以非常方便的将外部的类引入spring容器管理,比如你引用了第三方的jar到自己的spring项目中,需要spring管理第三方jar中的写Bean实例则非常方便。但是必须借助Import注解导入来实现,Import注解的参数可以是一个类,也可以是一个ImportSelector或ImportBeanDefinitionRegistrar接口。如果是一个类,会创建一个该类对应的Bean到IOC容器,如果是Import接口,则会将对应实现逻辑中的Bean加载的容器中。接口源码:

public interface ImportSelector {

	/**
	 * 返回需要导入类的全类名,在@Configuration标注的Class上可以使用@Import引入实现类,spring会将返回的类实例化为Bean托管
	 */
	String[] selectImports(AnnotationMetadata importingClassMetadata);

}

5. ImportSelectorRegistaror接口

ImportSelectorRegistaror同上借助Import注解实现Bean的动态注册,不同的是ImportSelector 只需返回全类名由spring转化为BeanDefinition,而ImportSelectorRegistaror需要自己将BeanDefinition注册到容器中。Spring AOP、AutoProxyRegistrar、Feign、Ribbon、Mybatis等都利用它实现的。

public interface ImportBeanDefinitionRegistrar {

	/**
	 * 注册BeanDefinition实现Bean的动态注入
	 */
	public void registerBeanDefinitions(
			AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);

}
发布了105 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43792385/article/details/103977628