BeanFactory & ApplicationContext

org.springframework.beans.factory.BeanFactory 是最基本的 Spring 容器接口,它提供了管理 Bean 的一些基本功能。 BeanFactory 接口包含如下几个基本方法:
  Object getBean(String name) throws BeansException;
  <T> T getBean(String name, Class<T> requiredType) throws BeansException;
  boolean containsBean(String name);
  Class<?> getType(String name) throws NoSuchBeanDefinitionException;

org.springframework.context.ApplicationContext 是 org.springframework.beans.factory.BeanFactory 的子接口,除了提供 BeanFactory 所支持的功能,ApplicationContext 还提供了一些额外的功能,如国际化支持、事件机制、资源访问等。

Spring 配置文件的基本结构

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="..." class="..." >
<property name="..." value="..." />
<property name="..." ref="..." />
</bean> </beans>

实例化容器

按文件系统路径加载配置文件:

ApplicationContext appCtx = new FileSystemXmlApplicationContext("src/applicationContext.xml");

按类路径加载配置多件:

ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");

加载多个配置文件:

ApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

使用 Spring 容器

ExampleBean example = appCtx.getBean("example", ExampleBean.class);

Spring(3.2.3) - Beans(1): Spring 容器的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->关于spring framework中的beans

    Spring framework中的beans 1.概述 bean其实就是各个类实例化后的对象,即objects spring framework的IOC容器所管理的基本单元就是bean spring ...

  2. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

  3. 监听器如何获取Spring配置文件(加载生成Spring容器)

    Spring容器是生成Bean的工厂,我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个 ...

  4. Spring源码学习(6)——容器的功能扩展

    之前的随笔中借BeanFactory介绍了bean的解析和加载的完整过程,实际上,除了BeanFactory,spring还提供了一种功能更加强大的容器:ApplicationContext Appl ...

  5. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  6. spring源码深度解析— IOC 之 容器的基本实现

    概述 上一篇我们搭建完Spring源码阅读环境,spring源码深度解析—Spring的整体架构和环境搭建 这篇我们开始真正的阅读Spring的源码,分析spring的源码之前我们先来简单回顾下spr ...

  7. Spring源码-IOC部分-自定义IOC容器及Bean解析注册【4】

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...

  8. Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析

    Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...

  9. Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理

    Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理 前言 上一篇分析了BeanFactoryPostProcessor的作用,那么这一篇继续 ...

随机推荐

  1. HDU 5723 Abandoned country (最小生成树+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

  2. How to organize the Template Files in C++

    Normally you put class definitions in a header file and method definitions in a source file. Code th ...

  3. Enterprise Library 服务问题

    在使用Enterprise Library而没有注册服务的时候会出现这样的问题,"Editing Post "Failed to create instances of perfo ...

  4. Vue2.0表单校验组件vee-validate的使用

    vee-validate使用教程 *本文适合有一定Vue2.0基础的同学参考,根据项目的实际情况来使用,关于Vue的使用不做多余解释.本人也是一边学习一边使用,如果错误之处敬请批评指出* 一.安装 n ...

  5. PC/UVa 题号: 110104/706 LC-Display (液晶显示屏)题解

    #include <string> #include <iostream> #include <cstring> #include <algorithm> ...

  6. 算法代码[置顶] 机器学习实战之KNN算法详解

    改章节笔者在深圳喝咖啡的时候突然想到的...之前就有想写几篇关于算法代码的文章,所以回家到以后就奋笔疾书的写出来发表了 前一段时间介绍了Kmeans聚类,而KNN这个算法刚好是聚类以后经常使用的匹配技 ...

  7. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...

  8. [AngularJS] Using $anchorScroll

    If you're in a scenario where you want to disable the auto scrolling, but you want to control the sc ...

  9. Android下将图片载入到内存中

    Android的系统的标准默认每一个应用程序分配的内存是16M.所以来说是很宝贵的,在创建应用的时候要尽可能的去节省内存,可是在载入一些大的文件的时候,比方图片是相当耗内存的,一个1.3M的图片,分辨 ...

  10. 免费的天气预报API--谷歌,雅虎,中央气象台

    Google Weather API 仅仅支持美国地区使用邮政编码进行查询,比如:  http://www.google.com/ig/api?hl=zh-cn&weather=94043  ...