Alex刺客

          Dancing fingers, damage world. -- 舞動手指,破壞世界.

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            57 隨筆 :: 0 文章 :: 76 評論 :: 0 Trackbacks
          本文參考了
          http://www.springsource.org/documentation
          spring 2.5中文參考手冊


          一、IoC 容器
          ?
          ?? ?1.org.springframework.beans.factory.BeanFactory 是Spring IoC容器的實際代表者,IoC容器負責容納此前所描述的bean,并對bean進行管理。

          ?? ?2.在Spring中,BeanFactory是IoC容器的核心接口。 它的職責包括:實例化、定位、配置應用程序中的對象及建立這些對象間的依賴。

          ?? ?3.Spring為我們提供了許多易用的BeanFactory實現, XmlBeanFactory就是最常用的一個。該實現將以XML方式描述組成應用的對象 以及對象間的依賴關系。XmlBeanFactory類將獲取此XML配 置元數據,并用它來構建一個完全可配置的系統或應用。

          ?? ?4.BeanFactory 容器提供配制框架及基本功能,。ApplicationContext 容器 是 BeanFactory 的擴展,集成了 Spring AOP、資源處理(國際化處理)、事件傳遞及各種不同應用層的context實現 (如針對web應用的WebApplicationContext)。
          ?
          ?
          二、Bean
          ?
          ?? ?在Spring中,那些組成你應用程序的主體(backbone)及由Spring IoC容器所管理的對象,被稱之為bean。 簡單地講,bean就是由Spring容器初始化、裝配及管理的對象,除此之外,bean就與應用程序中的其他對象沒有什么區別了。 而bean定義以及bean相互間的依賴關系將通過配置元數據來描述。
          ?
          ??? ?
          三、用 XML 定義Bean
          ?1?<?xml?version="1.0"?encoding="UTF-8"?>
          ?2?<beans?xmlns="http://www.springframework.org/schema/beans"
          ?3?????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ?4?????????xsi:schemaLocation="http://www.springframework.org/schema/beans
          ?5?????????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          ?6?
          ?7?????????<bean?id="BeanID"?class="ClassPath">
          ?8?????????</bean>
          ?9?
          10?????????<bean?id=""?class="">
          11?????????</bean>
          12?
          13?????????
          14?
          15?</beans>
          16?

          ?bean定義與應用程序中實際使用的對象一一對應。通常情況下bean的定義包括:服務 層對象、數據
          訪問層對象(DAO)、類似Struts Action的 表示層對象、Hibernate SessionFactory對象、JMS Queue對象>等等。通常bean的定義并不與容器中的領域 對象相同,因為領域對象的創建和加載必須依賴具體的DAO和業>務邏輯。


          四、實例化容器

          ??????? 1.實例化 BeanFactory 容器
          1?Resource?resource?=?new?ClassPathResource("alex/study/spring/spring.xml")
          2?BeanFactory?factory?=?new?XmlBeanFactory(resource);
          ?
          ?????? 2.實例化 ApplicationContext 容器
          1?ApplicationContext?factory?=?new?ClassPathXmlApplicationContext("alex/study/spring/spring.xml");


          五、多個 xml 配置文件

          ??????? 1.指定一個數組
          1?ApplicationContext?context?=?new?ClassPathXmlApplicationContext(new?String[]?{"services.xml",?"daos.xml"});

          ??????? 2.在 xml 配置文件 Beans 塊首部引入其它配置文件
          ?1?<beans>
          ?2?
          ?3?????<import?resource="services.xml"/>
          ?4?????<import?resource="resources/messageSource.xml"/>
          ?5?????<import?resource="/resources/themeSource.xml"/>
          ?6?
          ?7?????<bean?id="bean1"?class=""/>
          ?8?????<bean?id="bean2"?class=""/>
          ?9?
          10?</beans>


          六、Bean 別名

          ??????? 1.每個bean都有一個id,這些id在當前IoC容器中必須唯一。指定別名可以在name屬性中使用逗號、>空格將多個字符串(別名)分隔。
          1?<bean?id="hello"?name="world,hello2?hello3"?class="alex.study.spring.beanlifecycle.HelloService"/>

          ??????? 2.或使用alias元素配置
          1?<alias?name="hello"?alias="helloalias"/>


          七、實例化 Bean
          ??????? 1.默認的 Bean 通過類的默認(無參)構造器創建實例。

          ??????? 2.使用靜態工廠方法實例化,createInstance()必須是一個static方法。
          1?<bean?id="exampleBean"?class="examples.ExampleBean2"?factory-method="createInstance"/>

          ??????? 3.使用實例工廠方法實例化.與 使用靜態工廠方法實例化類似,用來進行實例化的非靜態實例工廠>方法位 于另外一個bean中,容器將調用該bean的工廠方法來創建一個新的bean實例。為使 用此機制,class屬性必須為空,而factory-bean 屬性必須指定為當前(或其祖先)容器中包含工廠方法的bean的名稱,而該 >工廠bean的工廠方法本身必須通過factory-method屬性來設定。
          1?<bean?id="serviceLocator"?class="com.foo.DefaultServiceLocator"></bean>
          2?<bean?id="exampleBean"?factory-bean="serviceLocator"?factory-method="createInstance"/>


          posted on 2010-07-30 09:07 Alex刺客 閱讀(325) 評論(0)  編輯  收藏 所屬分類: Spring Study Notes

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 呼和浩特市| 张北县| 秭归县| 东莞市| 宜昌市| 灵武市| 绍兴市| 邻水| 田林县| 深泽县| 定州市| 天门市| 会东县| 茂名市| 大足县| 沂源县| 阳山县| 太湖县| 肇源县| 堆龙德庆县| 丰宁| 建水县| 章丘市| 大化| 民权县| 塔城市| 永兴县| 抚宁县| 孝昌县| 太保市| 花垣县| 江口县| 宽城| 临漳县| 芦山县| 安宁市| 称多县| 曲周县| 静乐县| 二手房| 沛县|