`

spring AbstractApplicationContext.regilsterBeanPostProcessors

 
阅读更多

 

regilsterBeanPostProcessors 方法也是在容器初始化时调用,主要是注册BeanPostProcessor。

BeanPostProcessor 在spring中扮演了重要的角色它提供了扩展spring的可能。如Aop,spring事物都是基于BeanPostProcessor ,BeanPostProcessor 作用就是再生成Bean时对bean进行加强处理如生成代理对象。。

public interface BeanPostProcessor {

	//bean初始化前
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
      //bean初始化后
       Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

 

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		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.
		List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
		List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
		List<String> orderedPostProcessorNames = new ArrayList<String>();
		List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
		for (String ppName : postProcessorNames) {
			if (isTypeMatch(ppName, PriorityOrdered.class)) {
				BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
				priorityOrderedPostProcessors.add(pp);
				if (pp instanceof MergedBeanDefinitionPostProcessor) {
					internalPostProcessors.add(pp);
				}
			}
			else if (isTypeMatch(ppName, Ordered.class)) {
				orderedPostProcessorNames.add(ppName);
			}
			else {
				nonOrderedPostProcessorNames.add(ppName);
			}
		}

		// First, register the BeanPostProcessors that implement PriorityOrdered.
		OrderComparator.sort(priorityOrderedPostProcessors);
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		// Next, register the BeanPostProcessors that implement Ordered.
		List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
		for (String ppName : orderedPostProcessorNames) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			orderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		OrderComparator.sort(orderedPostProcessors);
		registerBeanPostProcessors(beanFactory, orderedPostProcessors);

		// Now, register all regular BeanPostProcessors.
		List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
		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.
		OrderComparator.sort(internalPostProcessors);
		registerBeanPostProcessors(beanFactory, internalPostProcessors);
 //添加处理实现了ApplicationListener接口类,放入applicationEventMulticaster中
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector());
	}

 

 

 

 

分享到:
评论

相关推荐

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    第14章:网络编程(day20).zip

    [[03 08:58:22,466 INFO ] org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:511) - Refreshing WebApplicationContext for namespace 'hessian-...

    struts2驱动包

    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729) at org.springframework.context.support.AbstractApplicationContext...

    testSpring

    2013-3-20 22:22:46 org.springframework.context.support.AbstractApplicationContext refresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e: display name [org....

    spring容器的触发事件 ClassPathXmlApplicationContext的start()方法的用法

    AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml"); ctx.start(); // ContextStartedEvent 发生 ctx.refresh(); // ContextRefreshedEvent 发生 ctx.stop...

    Spring启动流程.java

    AbstractApplicationContext.refresh(){ 1.为刷新准备此上下文 prepareRefresh(){ initPropertySources();//在上下文环境中初始化任何占位符属性源 getEnvironment().validateRequiredProperties();//验证所有...

    Spring源码refresh方法调试笔记

    - `AbstractApplicationContext.getEnvironment()`:返回当前的环境对象,这里获取的是`StandardServletEnvironment`。 - 初始化属性源。 - `StandardServletEnvironment.initPropertySources(null, null)`:初始...

    spring-framework-5.3.29.tar.gz

    其子类AbstractApplicationContext实现了上下文的功能,提供事件发布、资源加载等功能。 2. 依赖注入(Dependency Injection):在`org.springframework.beans.factory.annotation`包下,有如...

    18 Spring IoC容器如何读取应用外部的xml,txt,图形或者属性文件?慕课专栏(1)1

    3. `AbstractApplicationContext.refresh`刷新容器,加载并解析配置文件。 4. `AbstractRefreshableApplicationContext.refreshBeanFactory`刷新Bean工厂。 5. `AbstractXmlApplicationContext.loadBeanDefinitions`...

    SPRING API 2.0.CHM

    AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory.AspectJAnnotation AbstractAspectJAdvisorFactory....

    Spring Aop 源码流程.doc

    `refresh()`方法位于`AbstractApplicationContext`类中,它负责加载和刷新配置,以及初始化Bean工厂。在这个过程中,`refresh()`会调用`invokeBeanFactoryPostProcessors(beanFactory)`,目的是处理所有的`...

    SpringCodeReadingNotes:Spring源码阅读笔记-spring源码阅读

    Spring源码阅读笔记 以Spring 4.2.9版本为基础,进行以下探索。 1.1 Spring是如何处理事务的 即先@Transactional注解是如何完成的。我们先创建一个类TestService,加上@Service和...从AbstractApplicationContext.refre

    Spring Bean创建初始化流程.docx

    调用`refresh()`方法,这是在`AbstractApplicationContext`类中定义的。此方法负责加载配置、创建Bean工厂以及启动事件监听器等。 3. **完成Bean工厂初始化**: 在`refresh()`方法内部,调用`...

    spring4.0库

    它还包含BeanDefinition,用于存储Bean的元数据,以及BeanFactory和AbstractApplicationContext,是Spring IoC容器的基础。 4. **spring-web-4.0.0.RELEASE.jar**:该模块是Spring框架的Web支持部分,提供了与...

    spring第三天.pdf

    课程还会涵盖Spring容器的初始化流程,包括`AbstractApplicationContext`的`refresh()`方法,这是Spring应用上下文启动的关键。在web环境中,通常由`ContextLoaderListener`监听器来触发这个过程。 总的来说,本...

    Spring IOC设计原理解析.docx

    - `AbstractApplicationContext`的`refreshBeanFactory()`方法加载Bean定义。 - `loadBeanDefinitions()`方法解析Bean定义资源。 - `AbstractBeanDefinitionReader`读取并解析Bean定义。 - 资源加载器读取配置...

    spring IOC源码 – 单实例bean的循环依赖 源码及过程

    当我们尝试从ApplicationContext获取`Water`对象时,Spring首先调用`AbstractApplicationContext`的`getBean(Water.class)`方法。此方法进一步调用`BeanFactory`的`doGetBean()`来实例化`Water`。 2. **三级缓存...

    Spring获取webapplicationcontext,applicationcontext几种方法详解

    ` private AbstractApplicationContext appContext;` ` public synchronized static AppContext getInstance() {` ` if (instance == null) {` ` instance = new AppContext();` ` }` ` return instance;` ` }` ` ...

    springAOP核心组件分析.pdf

    AnnotationAwareAspectJAutoProxyCreator的创建和注册是通过AbstractApplicationContext类的registerBeanPostProcessors()方法实现的。在该方法中,会调用BeanFactory的getBean()方法来创建BeanPostProcessor的实例...

    spring学习笔记1

    - 在Web环境中,Singleton Bean的销毁方法可以通过`AbstractApplicationContext.registerShutdownHook()`来注册。 #### 五、工厂Bean - **工厂Bean**:在Spring中,可以通过实现`FactoryBean`接口来自定义Bean的...

Global site tag (gtag.js) - Google Analytics