首页 科技正文

联博接口:spring源码剖析——BeanPostProcessor接口

admin 科技 2020-06-19 90 0

 

  BeanPostProcessor是处置bean的后置接口,beanDefinitionMaps中的BeanDefinition实例化完成后,完成populateBean,属性设置,完成

初始化后,这个接口支持对bean做自界说的操作。

一:BeanPostProcessor的使用

界说一个测试用的model工具,name属性默以为hello

public class BeanDemo {

	private String name = "hello";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("BeanDemo{");
		sb.append("name='").append(name).append('\'');
		sb.append('}');
		return sb.toString();
	}
}

  

自界说一个MyBeanPostProcessor类,实现BeanPostProcessor接口

@Service
public class MyBeanPostProcessor implements BeanPostProcessor {

	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return null;
	}

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(beanName.equals("beanDemo")){
			BeanDemo beanDemo = (BeanDemo)bean;
			beanDemo.setName("kitty");
			return beanDemo;
		}
		return bean;
	}
}

  

 

 

从运行效果看,spring中维护的beanName为beanDemo的工具,name属性为ketty

 

 

二:看看源码怎么实现的

1:实例化而且注册到beanPostProcessors聚集中

 

 

 

 

主要的实例化逻辑在这个接口,这个接口的作用就是把所有实现BeanPostProcessor接口的类实例化,然后注册到 beanPostProcessors这个缓存中

 

 

	public static void registerBeanPostProcessors(
			ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

		// 获取所有实现接口BeanPostProcessor的beanName
		String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

		// Register BeanPostProcessorChecker that logs an info message when
		// a bean is created during BeanPostProcessor instantiation, i.e. when
		// a bean is not eligible for getting processed by all BeanPostProcessors.
		int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
		beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

		// Separate between BeanPostProcessors that implement PriorityOrdered,
		// Ordered, and the rest.
		/**
		 * 把实现PriorityOrdered 和 Ordered 和 其他的处置器离开
		 */
		List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
		List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
		List<String> orderedPostProcessorNames = new ArrayList<>();
		List<String> nonOrderedPostProcessorNames = new ArrayList<>();
		/**
		 * 1:遍历聚集postProcessorNames
		 * 2:判断类型,如果是PriorityOrdered,则实例化工具放入priorityOrderedPostProcessors聚集,
		 * Ordered 则放入orderedPostProcessorNames聚集,其他的放入nonOrderedPostProcessorNames聚集
 		 */
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
				priorityOrderedPostProcessors.add(pp);
				if (pp instanceof MergedBeanDefinitionPostProcessor) {
					internalPostProcessors.add(pp);
				}
			}
			else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
				orderedPostProcessorNames.add(ppName);
			}
			else {
				nonOrderedPostProcessorNames.add(ppName);
			}
		}

		// First, register the BeanPostProcessors that implement PriorityOrdered.
		// 首先对priorityOrderedPostProcessors聚集中实例工具排序,然后注册,放入beanFactory中缓存下来
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		// Next, register the BeanPostProcessors that implement Ordered.
		// 然后再实例化实现Ordered接口的工具,完成注册
		List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
		for (String ppName : orderedPostProcessorNames) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			orderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		sortPostProcessors(orderedPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, orderedPostProcessors);

		// Now, register all regular BeanPostProcessors.
		// 最后实例化什么都没有实现的,完成实例化并注册
		List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
		for (String ppName : nonOrderedPostProcessorNames) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			nonOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

		// Finally, re-register all internal BeanPostProcessors.
		// 最后再次注册内部postProcessor
		sortPostProcessors(internalPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, internalPostProcessors);

		// Re-register post-processor for detecting inner beans as ApplicationListeners,
		// moving it to the end of the processor chain (for picking up proxies etc).
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
	}

  

 

 

界说四类容器,高优先级有序、有序、无序、内部

 

 分类放入四种容器:

 

 

注册BeanPostProcessor,将实现BeanPostProcessor接口的工具放入beanPostProcessors缓存中

 

 

 

 

 

 

注册完PriorityOrdered的实现类后,再处置Ordered的实现类

 

 

注册什么都没有实现的BeanPostProcessor接口实现类,

 

 

最后注册内部的BeanPostProcessor工具

 

 到这里BeanPostProcessor的实例化以及注册事情完成,在beanFactory的beanPostProcessors聚集中已经缓存了所有的beanPostProcessor的工具

 

2:BeanPostProcessor的使用

由于这个接口是bean的后置接口,以是需要bean建立并初始化完成,才可以发挥作用,上一步的缓存只是埋好点,以备使用,由于bean的实例化流程我们

还没有剖析,这里直接看一下怎么使用的

 

 

我们看一下init方式后的阻挡,由于这个时刻已经init完成,可以在后置接口中对bean做一下修改的操作

 

 

挪用到我们自界说的MyBeanPostProcessor实现类:

 

 

把这个beanDemo工具属性修改一下,修改完,再返回,将这个工具缓存到spring的一级缓存中。

 

 

总结:

  BeanPostProcessor接口主要是对bean工具做一些自界说的操作,修改bean工具的信息,aop署理也是通过这种方式实现的,

在refresh的registryBeanPostProcessor方式中实例化BeanPostProcessor工具,而且注册到beanFactory容器的beanPostProcessors的缓存中,

然后在后续的操作中阻挡使用。

 

,

Allbet Gmaing官网

欢迎进入Allbet Gmaing官网(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。

版权声明

本文仅代表作者观点,
不代表本站Allbet Gaming的立场。
本文系作者授权发表,未经许可,不得转载。

评论

标签列表

    站点信息

    • 文章总数:539
    • 页面总数:0
    • 分类总数:8
    • 标签总数:699
    • 评论总数:373
    • 浏览总数:44321

    文章归档