<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    MicroFish

    Open & Open hits
    隨筆 - 33, 文章 - 2, 評(píng)論 - 4, 引用 - 0
    數(shù)據(jù)加載中……

    《Pro Spring》學(xué)習(xí)筆記(9)--基礎(chǔ)進(jìn)階(4)

    ?

    ????? 到目前為止,我們都是使用BeanFactory接口以及它的子接口來(lái)使用Spring,盡管使用BeanFactory也是一種不錯(cuò)的方式,但正如前面看到的,有些時(shí)候,我們還是不得不自己編寫代碼來(lái)調(diào)用諸如preInstantiateSingletons,postProcessBeanFactory等方法。
    ????? 為了使用的方便,Spring提供了BeanFactory的一個(gè)擴(kuò)展:ApplicationContext。使用ApplicationContext,我們可以減少需要編寫的代碼的數(shù)量,并且ApplicationContext也增加了一些新的功能。如果在Web工程中使用Spring,我們甚至可以讓Spring自動(dòng)加載ApplicationContext,而無(wú)需自己編寫代碼來(lái)創(chuàng)建它。
    ????? ApplicationContext具備一些BeanFactory不具備的功能:國(guó)際化(Internationalization),事件發(fā)布(Event publication),資源管理和訪問(wèn)(Resource management and access),更多的生命周期控制接口(Additional lifecycle interfaces)以及底層組件的自動(dòng)配置(Improved automatic configuration of infrastructure components)。
    ????? Spring為ApplicationContext接口提供了三種實(shí)現(xiàn):FileSystemXmlApplicationContext,ClasspathXmlApplicationContext和XmlWebApplicationContext。其中XmlWebApplicationContext是專為Web工程定制的,并且我們可以使用ContextLoaderListener或ContextLoaderServlet來(lái)自動(dòng)加載ApplicationContext配置。
    ????? 通常國(guó)際化是通過(guò)使用MessageSource接口來(lái)實(shí)現(xiàn)的,為了在ApplicationContext中使用MessageSource,我們需要配置一個(gè)類型為MessageSource,名稱為messageSource的bean。
    <beans>
    ??? <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    ??????? <property name="basenames">
    ??????????? <list>
    ??????????????? <value>buttons</value>
    ??????????????? <value>labels</value>
    ??????????? </list>
    ??????? </property>
    ???? </bean>
    </beans>
    ????? 在配置了messageSource后,我們就可以使用ApplicationContext的getMessage方法來(lái)獲取資源內(nèi)容了。
    ????? ApplicationContext的另一個(gè)重要特性就是對(duì)發(fā)布和接收事件的支持,ApplicationContext會(huì)在其被配置的時(shí)候自動(dòng)注冊(cè)所有的偵聽(tīng)器(listener),事件的發(fā)布是通過(guò)ApplicationContext的publishEvent方法來(lái)實(shí)現(xiàn)的。
    //Event class
    public class MessageEvent extends ApplicationEvent {
    ??? private String msg;
    ??? public MessageEvent(Object source, String msg) {
    ??????? super(source);
    ??????? this.msg = msg;
    ??? }
    ??? public String getMessage() {
    ??????? return msg;
    ??? }
    }
    //Listener class
    public class MessageEventListener implements ApplicationListener {
    ??? public void onApplicationEvent(ApplicationEvent event) {
    ??????? if(event instanceof MessageEvent) {
    ??????????? MessageEvent msgEvt = (MessageEvent)event;
    ??????????? System.out.println("Received: " + msgEvt.getMessage());
    ??????? }
    ??? }
    }
    //Publish class
    public class Publisher implements ApplicationContextAware {
    ??? private ApplicationContext ctx;
    ??? public static void main(String[] args) {
    ??????? ApplicationContext ctx =
    ??????????????? new FileSystemXmlApplicationContext("./ch5/src/conf/events/events.xml");
    ??????? Publisher pub = (Publisher) ctx.getBean("publisher");
    ??????? pub.publish("Hello World!");
    ??????? pub.publish("The quick brown fox jumped over the lazy dog");
    ??? }
    ??? public void setApplicationContext(ApplicationContext applicationContext)
    ??????????? throws BeansException {
    ??????? this.ctx = applicationContext;
    ??? }
    ??? public void publish(String message) {
    ??????? ctx.publishEvent(new MessageEvent(this, message));
    ??? }
    }
    <beans>
    ??? <bean id="publisher" class="com.apress.prospring.ch5.event.Publisher"/>
    ??? <bean id="messageEventListener" class="com.apress.prospring.ch5.event.MessageEventListener"/>
    </beans>
    ????? 資源訪問(wèn)是我們經(jīng)常碰到的,這些資源有可能存在于一個(gè)文件中,類路徑中的一個(gè)jar包中,抑或是在遠(yuǎn)程服務(wù)器上。Spring為我們提供了統(tǒng)一的,協(xié)議無(wú)關(guān)的方式來(lái)訪問(wèn)各種各樣的資源,這給我們?cè)L問(wèn)各種資源提供了極大的方便。
    public class ResourceDemo {
    ??? public static void main(String[] args) throws Exception {
    ??????? ApplicationContext ctx =
    ??????????????? new FileSystemXmlApplicationContext("./ch5/src/conf/events/events.xml");
    ??????? Resource res1 = ctx.getResource("
    file:///d:/tmp/test.txt ");
    ??????? displayInfo(res1);
    ??????? Resource res2 = ctx.getResource("classpath:lib/commons-logging.jar");
    ??????? displayInfo(res2);
    ??????? Resource res3 = ctx.getResource("
    http://www.google.co.uk ");
    ??????? displayInfo(res3);
    ??? }
    ??? private static void displayInfo(Resource res) throws Exception {
    ??????? System.out.println(res.getClass());
    ??????? System.out.println(res.getURL().getContent());
    ??????? System.out.println("");
    ??? }
    }

    posted on 2006-12-21 10:25 劉璐 閱讀(304) 評(píng)論(0)  編輯  收藏 所屬分類: spring

    主站蜘蛛池模板: 精品一区二区三区免费毛片爱 | 亚洲精品视频在线看| 亚洲成人免费网站| **一级一级毛片免费观看| 亚洲一区影音先锋色资源| 中文字幕成人免费视频| 亚洲日韩乱码中文无码蜜桃| 亚洲免费视频观看| 亚洲AV色吊丝无码| 免费无码一区二区三区蜜桃大 | 亚洲国产电影在线观看| 在线v片免费观看视频| 亚洲娇小性色xxxx| 亚洲国产成人久久| 国产一精品一AV一免费孕妇| 亚洲高清视频一视频二视频三| 久久精品亚洲中文字幕无码麻豆| 99re在线视频免费观看| 亚洲国产综合在线| 成人永久福利免费观看| 一级毛片a女人刺激视频免费| 国产亚洲日韩在线三区| 99视频在线精品免费| 亚洲免费视频网址| 免费va人成视频网站全| 国产一级a毛一级a看免费人娇| 国产成人免费片在线观看| 日本高清免费中文在线看| 亚洲精品中文字幕乱码三区| 国产成人精品免费午夜app | 亚洲av产在线精品亚洲第一站| 青青草国产免费久久久下载| 一个人看的www视频免费在线观看| 亚洲精品狼友在线播放| 日本片免费观看一区二区| 爱情岛亚洲论坛在线观看| 九月丁香婷婷亚洲综合色| 三级毛片在线免费观看| 亚洲人成电影在线观看青青| 亚洲国产天堂久久综合| **实干一级毛片aa免费|