首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring核心技术-IoC的Bean根本管理

2012-11-15 
Spring核心技术----IoC的Bean基本管理BeanFactory?? 和??? ApplicationContext???????????? org.springfra

Spring核心技术----IoC的Bean基本管理

BeanFactory?? 和??? ApplicationContext

?

??????????? org.springframework.beans.factory.BeanFactory 负责读取Bean定义文件,管理对象的加载,生成,维护Bean对象与Bean对象之间的依赖关系,负责Bean的生命周期。

?????????????

???????????? org.springframework.context.ApplicationContext??的基本功能与BeanFactory很相似,它也具有负责读取Bean定义文件,维护Bean之间的依赖关系等功能,除此之外ApplicationContext还提供了一个应用程序所需的更完整的框架功能,例如:

提供取得资源文件(Resource file)更方便的方法;提供文字消息解析的方法;支持国际化(Internationalization ,I18N)消息;ApplicationContext可以发布事件,对事件感兴趣的Bean可以接收到这些事件;

?Spring的创始人Rod Johnson建议使用ApplicationContext来取代BeanFactory,在 实现ApplicationContext的类中,最常用的大概是以下3个:

org.springframework.context.support.FileSystemXmlApplicationContext

?????????/**?可指定XML定义文件的相对路径或绝对路径来读取定义文件*/

org.springframework.context.support.ClassPathXmlApplicationContext

?????????/** 从Classpath设定路径来读取XML定义文件*/

org.springframework.web.context.support.XmlWebApplicationContect

???????/**? 在Web应用程序的文件架构中,指定相对位置来读取定义文件*/

?

?

==============

修改前一个例子的代码

-------------------------------------------

package only;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

??????? public class SpringDemo {

?????????????????? ?public static void main(String[] args) {
?????????????????????????? ApplicationContext context=

????????????????????????????????????????????? new FileSystemXmlApplicationContext("src/applicationContext.xml");
???????????????????????????HelloBean hello=(HelloBean)context.getBean("helloBean");? //获得Bean对象
?????????????????????????? System.out.println(hello.getHelloWord()); //应用Bean对象
????????????????????}
?????????}

热点排行