使用Spring容器

    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時將擁有較好性能。

相關文章
相關標籤/搜索