结合Spring IOC 看Webx & HSF & Spring MVC

本文深入探讨Spring的IOC流程,包括BeanFactoryPostProcessor、Aware和BeanPostProcessor等,同时讲解了阿里开源的Webx框架、HSF RPC框架与Spring的交互,以及在初始化过程中的重要作用。文章还介绍了Webx如何利用Spring的扩展点进行处理,以及Webx和SpringMVC的初始化流程比较。
摘要由CSDN通过智能技术生成

写在前面

Spring IOC这个部分还是很难的,我基本有了一年多的使用经验才慢慢开始看源码,但都是头大无比,一定要静下心慢慢看,就像一杯苦茶,越品越有味道!

Webx是阿里开源的web框架:
http://www.openwebx.org/docs/Webx3_Guide_Book.html
HSF是阿里的

IOC大致流程

IOC容器的两个最主要的两个接口BeanFactoryApplicationContext相信大家都接触过,IOC整个初始化流程非常复杂,分为两个主要的过程:

  1. 注册BeanDefinition
  2. getBean

Spring在复杂的流程中也为开发者留有一些接口,开发人员可以利用这些接口完成自己一些项目功能,或者在其之上继续开发框架。这部分也经常会以Spring生命周期的形式出现在各类资料上,那我们来疏导一下:

BeanFactoryPostProcessor

public interface BeanFactoryPostProcessor {

    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     * @param beanFactory the bean factory used by the application context
     * @throws org.springframework.beans.BeansException in case of errors
     */
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

BeanFactoryPostProcessor容器级操作,会在整个beanFactory初始化之后调用用户定义的BeanFactoryPostProcessor,例如placeholder,就是在BeanFactory注册好了之后对其中的placeholder进行替换。

Aware

顾名思义,这是spring提供的用户可以对spring容器的一些感知,常见的有ApplicationContextAware

    public interface ApplicationContextAware extends Aware {


    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

Aware的调用是被ApplicationContextAwareProcessor这个BeanPostProcessor在postProcessBeforeInitialization时调用的。有点绕,你可以先看下面的,然后再回来。

BeanPostProcessor

先看代码
public interface BeanPostProcessor {

    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

BeanPostProcessor是在执行用户定义的init-method前后执行before和after的,上例中的Aware就是在before的时候执行的。

InitializingBean

InitializingBean是在用户的init-method之前执行

public interface InitializingBean {

    void afterPropertiesSet() throws Exception;

}

有点乱了是不是,简单屡一下ApplicationContext的refresh过程,也就是初始化过程,首先会注册BeanFactory,然后执行BeanFactoryPostProcessor之后注册ApplicationContextAwareProcess

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值