Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口。它们均可以表明Spring容器,Spring容器是生成Bean实列的工厂,并管理容器中的Bean。java
Spring容器最基本的接口就是BeanFactory。BeanFactory负责配置、建立管理Bean,它有一个子接口:ApplictionContext,所以也被称为Spring上下文。web
BeanFactory接口包含如下几个基本方法:spring
@Override public boolean containsBean(String arg0) { // TODO Auto-generated method stub return false; } @Override public String[] getAliases(String arg0) { // TODO Auto-generated method stub return null; } @Override public Object getBean(String arg0) throws BeansException { // TODO Auto-generated method stub return null; } @Override public <T> T getBean(Class<T> arg0) throws BeansException { // TODO Auto-generated method stub return null; } @Override public <T> T getBean(String arg0, Class<T> arg1) throws BeansException { // TODO Auto-generated method stub return null; } @Override public Object getBean(String arg0, Object... arg1) throws BeansException { // TODO Auto-generated method stub return null; } @Override public Class<?> getType(String arg0) throws NoSuchBeanDefinitionException { // TODO Auto-generated method stub return null; } @Override public boolean isPrototype(String arg0) throws NoSuchBeanDefinitionException { // TODO Auto-generated method stub return false; } @Override public boolean isSingleton(String arg0) throws NoSuchBeanDefinitionException { // TODO Auto-generated method stub return false; } @Override public boolean isTypeMatch(String arg0, Class<?> arg1) throws NoSuchBeanDefinitionException { // TODO Auto-generated method stub return false; }
对于ApplicationContext是BeanFactory的子接口,它也是最经常使用的。其经常使用的实现类是FileSystemXmlApplicationContext,ClassPathXmlApplicationContext,AnntationConfigApplication。app
读取配置文件一般使用Resource对象传入,对于大部分WEB应用能够在项目启动时自动加载ApplicationContext实列。ide
在web.xml中配置:性能
<!-- 读取Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 用于初始化Spring容器的Listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
本身获取ApplicationContext对象有以下几种方法:spa
//搜索当前文件路径下的application-spring.xml,建立Resource对象 FileSystemResource isr = new FileSystemResource("application-spring.xml"); //建立BeanFactory实列 XmlBeanFactory xbf = new XmlBeanFactory(isr);
或者code
//搜索当前文件下的applicationContext-spring.xml并建立Resource对象 ClassPathResource cpr = new ClassPathResource("applicationContext-spring.xml"); //建立BeanFactory实列 XmlBeanFactory xbf = new XmlBeanFactory(cpr);
若是应用中有多个配置文件,则应该使用BeanFactory的子接口ApplicationContext来建立BeanFactory实列。ApplicationContext一般有以下两个实现类:xml
1)FileSystemXmlApplicationContext:基于文件系统的XML配置文件建立ApplicationContext实列;对象
2)ClassPathXmlApplicationContext:以类加载路径下的XML配置文件建立ApplicationContext实列;
ApplicationContext appContext = new FileSystemXmlApplicationContext(new String[]{"spring.xml", "spring1.xml"}); //或者 ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring1.xml"});
整理关系以下:
因为ApplicationContext是BeanFactory的子类,所以ApplicationContext彻底能够当作Spring容器,它加强了BeanFactory的功能,当系统建立ApplicationContext容器时,默认会预初始化全部的singleton Bean。这意味着:系统前期建立ApplicationContext时将有较大的系统开销,但一旦ApplicationContext初始化完成,程序后面获取singleton Bean时将拥有较好性能。