spring容器

什么是spring容器?

spring容器负责创建对象、装配对象,管理对象的整个生命周期。

都有哪些spring容器?

spring自带了多个容器实现,可以分为两种类型bean工厂和应用上下文ApplicationContext

beanFactory和应用上下文的区别?

beanFactory:

(1)实现了org.springframework.beans.Factory.beanFactory接口,提供基本的DI支持。

(2)beanFactory在创建Bean时,使用了延迟加载,在使用某个Bean时,才对bean进行实例化。

应用上下文:

(1)实现了org.springframework.context.ApplicationContext接口,基于beanFactory构建,并提供应用级别的服务,例如从属性文件解析文本信息

(2)在容器启动时,会一次性创建所有的bean,启动时间会比beanFactory时间长。

spring应用上下文

spring自带了多种类型的应用上下文(都为org.springframework.context.ApplicationContext接口的实现):

(1)AnnotationConfigApplicationContext:从一个或多个基于java的配置类中加载spring应用上下文

  举例:ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyCaseApplicationContext.class);

  从java配置中加载spring应用上下文。

(2)AnnotationConfigWebApplicationContext:从一个或多个基于java的配置类中加载spring web应用上下文

(3)ClassPathXmlApplicationContext:从类路径下的一个或多个xml配置文件中加载上下文定义,把应用上下文的定义文件作为类资源

  举例:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("properties.xml")

  从类路径下加载应用上下文。

(4)FileSystemXmlApplicationContext:从文件系统下的一个或多个xml配置文件中加载上下文定义,把应用上下文的定义文件作为类资源

  举例:ApplicationContext applicationContext = new FileSystemXmlApplicationContext("c:/properties.xml");

  从文件系统下的properties.xml文件中加载应用上下文。

(5)XmlWebApplicationContext:从web应用下的一个或者多个xml配置文件中加载上下文定义

posted @ 2019-05-15 10:27  第二人生Bonnie  阅读(822)  评论(0编辑  收藏  举报