Spring 容器
来源:互联网 发布:java调用函数里的参数 编辑:程序博客网 时间:2024/06/18 10:05
Spring 容器
Spring 容器负责创建对象,装配它们,配置它们并管理它们整个生命周期,从生存到死亡。容器是Spring框架的核心,Spring使用DI(依赖注入)管理构成应用的组件。
在Spring容器并不是只有一个。Spring自带了多个容器实现,可以归为两种类型:
- bean工厂即BeanFactory接口
- 应用上下文Application接口,基于BeanFactory构建,并提供应用框架级别的服务。
应用上下文分为五种类型:
- AnnotationConfigApplicationContext:从一个或多个基于Java的配置类中加载Spring应用上下文
- AnnotationConfigWwbApplicationContext:从一个或多个基于Java的配置类中加载Spring Web应用上下文。
- ClassPathXmlApplicationContext:从类路劲下的一个或多个XML配置文件中加载上下文定义,把应用上下文的定义文件作为类资源。
- FileSystemXmlApplicationContext:从文件系统下的一个或多个XML配置文件中加载上下文定义。
- XmlWebApplicationContext:从Web应用下的一个或多个XML配置文件中加载上下文定义。
public static void main(String[] args) { ApplicationContext context1 = new FileSystemXmlApplicationContext("c:/xxx.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("xxx.xml"); ApplicationContext context3 = new AnnotationConfigApplicationContext(com.bob.analyst.admin.aop.Role.class); //......}
应用上下文准备就绪后,我们可以调用上下文的getBean()方法从Spring容器中获取bean。
备注:
- FileSystemXmlApplicationContext 和 ClassPathXmlApplicationContext 的区别在于:
FileSystemXmlApplicationContext在指定的文件系统路劲下查找xxx.xml文件;
ClassPathXmlApplicationContext是在所有的类路劲下查找xxx.xml文件。 - AnnotationConfigApplicationContext是没有指定加载Spring应用上下文所需要的XML文件,它是通过一个配置类加载bean。
阅读全文
1 0
- spring容器
- Spring容器
- spring 容器
- Spring容器
- spring容器
- spring容器
- Spring容器
- spring 容器
- Spring容器
- spring容器
- Spring容器
- Spring容器
- Spring容器
- Spring容器
- Spring容器
- Spring容器
- Spring 容器
- spring容器
- Java8中流的性能
- HTML&CSS&DIV基础<2>
- 跟踪算法基准--Tracking the Trackers: An Analysis of the State of the Art in Multiple Object Tracking
- hashMap 全面解析
- 使用控件,dataGridView,如何使整个表格填满控件?
- Spring 容器
- 大数据1-Hadoop架构体系及在各方面的应用
- 为什么我发布的问答被抽了
- Data Binding基础入门
- AndroidStudio将branch的代码合并到master
- [编程题] 算法基础-字符移位
- HDU 2191 多重背包
- Java_实现单链表
- uvaol 156-------------map的使用