正如本章开头所讨论的那样,org.springframework.beans.factory包提供基本的功能来管理和操作bean,包括以编程的方式。The org.springframework.context包增加了ApplicationContext接口,它继承了BeanFactory接口,除了以面向应用框架的风
格扩展接口来提供一些额外的功能。很多人以完全声明的方式使用ApplicationContext,甚至没有以编程的方式去创建它,而是依赖诸如ContextLoader等支持类来自动的实例化ApplicationContext,作为Java EE web应用程序正常启动的一部分。
DefaultListableBeanFactory factory =newDefaultListableBeanFactory();// populate the factory with bean definitions// now register any needed BeanPostProcessor instances
MyBeanPostProcessor postProcessor =newMyBeanPostProcessor();
factory.addBeanPostProcessor(postProcessor);// now start using the factory
DefaultListableBeanFactory factory =newDefaultListableBeanFactory();
XmlBeanDefinitionReader reader =newXmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(newFileSystemResource("beans.xml"));// bring in some property values from a Properties file
PropertyPlaceholderConfigurer cfg =newPropertyPlaceholderConfigurer();
cfg.setLocation(newFileSystemResource("jdbc.properties"));// now actually do the replacement
cfg.postProcessBeanFactory(factory);