即興的靈感

          思維是一種藝術(shù); 藝術(shù)需要靈感。

          博客好友

          最新評(píng)論

          Spring筆記之一(初探Spring)

          ??? 初探Spring程序,程序如下:

          ?1? BeanNaming.java
          ?2?????package
          ?spring.beantest;
          ?3?

          ?4?????import ?org.springframework.beans.factory.BeanFactory;
          ?5?????import
          ?org.springframework.beans.factory.xml.XmlBeanFactory;
          ?6?????import
          ?org.springframework.core.io.FileSystemResource;
          ?7?

          ?8?????public?class ?BeanNaming?{
          ?9?????public?static?void
          ?main(String?[]args)
          10?
          ????{
          11?????????BeanFactory?factory?=?new?XmlBeanFactory(new?FileSystemResource("src/applicationContext.xml"
          ));
          12?
          ????
          13?????????String?s1?=?(String)factory.getBean("name1"
          );
          14?????????String?s2?=?(String)factory.getBean("name2"
          );
          15?????????String?s3?=?(String)factory.getBean("name3"
          );
          16?????????String?s4?=?(String)factory.getBean("name4"
          );
          17?
          ????????
          18?????????System.out.println((s1?==
          ?s2));
          19?????????System.out.println((s2?==
          ?s3));
          20?????????System.out.println((s3?==
          ?s4));
          21?
          ????????
          22?????????String[]?x?=?factory.getAliases("name3"
          );
          23?????????for
          (String?str:x)
          24?
          ????????{
          25?
          ????????????System.out.println(str);
          26?
          ????????}
          27?
          ????????
          28?
          ??????}
          29?
          ????}
          30?
          ?
          31?
          ??????applicationContext.xml
          32??????<?xml?version="1.0"?encoding="UTF-8"?>

          33??????<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">
          34??????<beans>
          35??????<!--?aliasing?examples?-->
          36??????<bean?id="name1"?name="name2,name3,name4"?class="java.lang.String"/>
          37??????</beans>

          ??? 輸出結(jié)果為:
          ??? true
          ??? true
          ??? true
          ??? name1
          ??? name4
          ??? name2

          ???
          ???
          可以看出,使用中id屬性和name屬性幾乎沒(méi)有任何區(qū)別。調(diào)用beanfactory.getAliases(string)的方法時(shí),傳入的參數(shù)可以是任意一個(gè)bean名字,輸出的別名則是除去作為參數(shù)本身之外的所有 bean名。
          ??

          ?? 在寫(xiě) applicationContxt.xml文件時(shí),如果沒(méi)有定義<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">,則會(huì)拋出Cannot find the declaration of element 'beans'異常。

          ??
          ?? ???
          以上使用BeanFactory對(duì)配置文件進(jìn)行加載,BeanFactory,是根據(jù)配置文件負(fù)責(zé)創(chuàng)建Bean的實(shí)例,并負(fù)責(zé)Bean的生命周期的管理- --,包括Bean的生成與銷毀、Bean的創(chuàng)建方式的識(shí)別(是否為singleton)、Bean的各個(gè)屬性的設(shè)定、依賴關(guān)系的建立等。
          ??? ApplicationContext接口,提供了國(guó)際化、事件處理及beans在context中的自查能力。它也可創(chuàng)建具有層次結(jié)構(gòu)context環(huán)境,將bean的作用域和可訪問(wèn)區(qū)域限制在應(yīng)用程序的一個(gè)特定部分中。下面演示如何使用ApplicationContext接口。


          ???
          ?1?<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-????beans.dtd">
          ?2?????<beans>
          ?3?????<bean?id="bean_1"?class="BeanTest"/>
          ?4?????<bean?id="bean_2"?class="BeanTest"/>
          ?5?????</beans>
          ?6?
          ?7?
          ?8?????public?class ?BeanTest?{
          ?9?????public?void
          ?test()?{
          10?????????System.out.println("test.."
          );
          11?
          ????}
          12?
          ???}
          13?

          14?
          15????import ?org.springframework.context.ApplicationContext;
          16????import
          ?org.springframework.context.support.FileSystemXmlApplicationContext;
          17?

          18????public?class ?BeanNameExample?{
          19?

          20?????public?static?void ?main(String[]?args)?{
          21?????????ApplicationContext?ctx?=?new
          ?FileSystemXmlApplicationContext(
          22?????????"build/applicationContext.xml"
          );
          23?
          ????????
          24?????????BeanTest?beanOne?=?(BeanTest)ctx.getBean("bean_1"
          );
          25?????????BeanTest?beanTwo?=?(BeanTest)ctx.getBean("bean_2"
          );
          26?
          ????????
          27?
          ????????beanOne.test();
          28?
          ????????beanTwo.test();
          29?
          ??????}
          30?
          ???}
          31?
          ?? 綜上,ApplicationContext與BeanFactory的不同點(diǎn)
          ?? BeanFactory提供了針對(duì)JavaBean的管理功能,而ApplicationContext提供了一個(gè)更為框架化的實(shí)現(xiàn)(從上面的示例中可以看出,BeanFactory的使用方式更加類似一個(gè)API,而非Framework style)。ApplicationContext覆蓋了BeanFactory的所有功能,并提供了更多的特性。此外, ApplicationContext為與現(xiàn)有應(yīng)用框架相整合,提供了更為開(kāi)放式的實(shí)現(xiàn)(如對(duì)于Web應(yīng)用,我們可以在web.xml中對(duì) ApplicationContext進(jìn)行配置)。


          ?
          鳳凰涅槃/浴火重生/馬不停蹄/只爭(zhēng)朝夕
          ???? 隱姓埋名/低調(diào)華麗/簡(jiǎn)單生活/完美人生

          posted on 2007-09-24 01:31 poetguo 閱讀(1960) 評(píng)論(5)  編輯  收藏 所屬分類: Spring

          評(píng)論

          # re: Spring筆記之一 2007-09-24 09:32 千里冰封

          我以前也想學(xué)spring,可就是一直學(xué)不進(jìn)去:(  回復(fù)  更多評(píng)論   

          # re: Spring筆記之一(初探Spring) 2007-09-24 10:58 improviser

          開(kāi)始研究...從現(xiàn)在開(kāi)始...  回復(fù)  更多評(píng)論   

          # re: Spring筆記之一(初探Spring) 2007-09-24 23:28 劉甘泉

          @千里冰封
          。。。全都回復(fù)的是這個(gè)。。spring會(huì)用就可以了,里面的具體代碼,有時(shí)間就看吧  回復(fù)  更多評(píng)論   

          # re: Spring筆記之一(初探Spring) 2007-10-22 17:19 悟空

          師傅 我就從你這里開(kāi)始 spring 拉  回復(fù)  更多評(píng)論   

          # re: Spring筆記之一(初探Spring) 2007-10-22 23:28 improviser

          哈哈 一起學(xué)習(xí) 共同進(jìn)步  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 天气| 黑龙江省| 岱山县| 永嘉县| 乾安县| 安阳市| 旌德县| 井陉县| 金坛市| 沂源县| 特克斯县| 汉寿县| 贡嘎县| 博客| 新疆| 孝义市| 乌兰察布市| 浙江省| 枣阳市| 嘉定区| 克什克腾旗| 海盐县| 石狮市| 喀什市| 凤庆县| 台山市| 金坛市| 临西县| 织金县| 岢岚县| 灌阳县| 扎赉特旗| 徐水县| 闽侯县| 凯里市| 故城县| 巩义市| 叙永县| 拜城县| 鹿泉市| 阜新|