Spring framework org.springframework.context包下提供了多种AbstractApplicationContext的实现,其中有几种比较常用的实现如下
常用ApplicationContext实现 | 说明 |
---|---|
FileSystemXmlApplicationContext | 从XML文件加载被定义的bean,访问路径为系统文件路径如C://beans.xml |
ClassPathXmlApplicationContext | 从XML文件加载被定义的bean,提供Classpath路径的ApplicationContext |
WebXmlApplicationContext | 从XML文件加载被定义的bean,加载路径基于整个Web工程 |
以上的几种AbstractApplicationContext实现的主要区别在于加载Xml定义文件的路径不同,但是加载bean的format都是XML的,且文件路径的表示方式单一,不能灵活改动。
从源码中的说明理解本类的特点和作用
Generic ApplicationContext implementation that holds a single internal DefaultListableBeanFactory instance and does not assume a specific bean definition format. Implements the BeanDefinitionRegistry interface in order to allow for applying any bean definition readers to it.
Generic ApplicationContext 持有一个DefaultListableBeanFactory实例,并且没有假设一个特定的bean definition 的format。实现了BeanDefinitionRegistry接口以允许配置任何bean definition reader(也可以不是XmlBeanDefinitionReader)。
Typical usage is to register a variety of bean definitions via the BeanDefinitionRegistry interface and then call AbstractApplicationContext.refresh() to initialize those beans with application context semantics (handling ApplicationContextAware, auto-detecting BeanFactoryPostProcessors, etc).
使用时通常要通过BeandefinitionRegistry接口注册一些bean definition然后调用AbstractApplicationContext.refresh()用application context的语义来初始化beans(如ApplicationContextAware和auto-detecting BeanFactoryPostProcessors等)。
In contrast to other ApplicationContext implementations that create a new internal BeanFactory instance for each refresh, the internal BeanFactory of this context is available right from the start, to be able to register bean definitions on it. AbstractApplicationContext.refresh() may only be called once.
和其他在每次refresh时都创建一个新的内部BeanFactory实例的ApplicationContext实例不同,本类中的BeanFactory从一开始就创建好并可在其中注册bean definitions,refresh方法可能在其中只调用一次
使用示例:
GenericApplicationContext ctx = new GenericApplicationContext();
//使用XmlBeanDefinitionReader
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
//加载ClassPathResource
xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
propReader.loadBeanDefinitions(new ClassPathResource("otherBeans.properties"));
//调用Refresh方法
ctx.refresh();
//和其他ApplicationContext方法一样的使用方式
MyBean myBean = (MyBean) ctx.getBean("myBean");
For the typical case of XML bean definitions, simply use ClassPathXmlApplicationContext or FileSystemXmlApplicationContext, which are easier to set up - but less flexible, since you can just use standard resource locations for XML bean definitions, rather than mixing arbitrary bean definition formats. The equivalent in a web environment is XmlWebApplicationContext.
For custom application context implementations that are supposed to read special bean definition formats in a refreshable manner, consider deriving from the AbstractRefreshableApplicationContext base class.
一般情况下使用ClassPathXmlApplicationContext或者
FileSystemXmlApplicationContext会比这个GenericApplicationContext更方便但相应地缺少灵活性,因为只能使用特定Bean definition 格式和加载路径。
要追求定制化的ApplicationContext实现,请阅读refreshable的说明中特殊bean definition 的格式,可从AbstractRefreshableApplicationContext继承。
总结
GenericApplicationContext不是比较常用的ApplicationContext实现,但是更具灵活性,可以任意配置读取路径和DefinitionReader,如果还需要比使用GenericApplicationContext更具灵活性的方式,还可以从比它更抽象的类来继承。总之,为了获得更多的灵活性,就要把框架本身拆得更散,框架本身也会提供合适的扩展方式来适应这种需求。
网友评论