<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接口以及它的子接口來使用Spring,盡管使用BeanFactory也是一種不錯(cuò)的方式,但正如前面看到的,有些時(shí)候,我們還是不得不自己編寫代碼來調(diào)用諸如preInstantiateSingletons,postProcessBeanFactory等方法。
    ????? 為了使用的方便,Spring提供了BeanFactory的一個(gè)擴(kuò)展:ApplicationContext。使用ApplicationContext,我們可以減少需要編寫的代碼的數(shù)量,并且ApplicationContext也增加了一些新的功能。如果在Web工程中使用Spring,我們甚至可以讓Spring自動(dòng)加載ApplicationContext,而無需自己編寫代碼來創(chuàng)建它。
    ????? ApplicationContext具備一些BeanFactory不具備的功能:國(guó)際化(Internationalization),事件發(fā)布(Event publication),資源管理和訪問(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來自動(dòng)加載ApplicationContext配置。
    ????? 通常國(guó)際化是通過使用MessageSource接口來實(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方法來獲取資源內(nèi)容了。
    ????? ApplicationContext的另一個(gè)重要特性就是對(duì)發(fā)布和接收事件的支持,ApplicationContext會(huì)在其被配置的時(shí)候自動(dòng)注冊(cè)所有的偵聽器(listener),事件的發(fā)布是通過ApplicationContext的publishEvent方法來實(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>
    ????? 資源訪問是我們經(jīng)常碰到的,這些資源有可能存在于一個(gè)文件中,類路徑中的一個(gè)jar包中,抑或是在遠(yuǎn)程服務(wù)器上。Spring為我們提供了統(tǒng)一的,協(xié)議無關(guān)的方式來訪問各種各樣的資源,這給我們?cè)L問各種資源提供了極大的方便。
    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 劉璐 閱讀(298) 評(píng)論(0)  編輯  收藏 所屬分類: spring

    主站蜘蛛池模板: 99精品全国免费观看视频| 在线看片v免费观看视频777 | 亚洲欧洲一区二区三区| 亚洲av无码专区在线观看素人| 亚洲一区二区三区影院| 亚洲国产精品lv| 亚洲一级毛片免费在线观看| 亚洲gay片在线gv网站| 久久久久久久久久久免费精品| 日本一卡精品视频免费| 成人免费视频小说| 亚洲色成人WWW永久网站| 老司机精品免费视频| 天堂在线免费观看中文版| 亚洲一区二区三区在线观看精品中文 | 亚洲欧洲另类春色校园网站| 看免费毛片天天看| 成全视频高清免费观看电视剧| 无码区日韩特区永久免费系列| 激情综合色五月丁香六月亚洲| 亚洲午夜在线一区| 日韩精品内射视频免费观看| 亚洲理论片在线观看| a在线视频免费观看在线视频三区| 少妇高潮太爽了在线观看免费| 国产成A人亚洲精V品无码| 偷自拍亚洲视频在线观看99| 亚洲精品国产精品国自产观看| 91免费在线视频| AV在线亚洲男人的天堂| 在线91精品亚洲网站精品成人| 国产亚洲精品福利在线无卡一 | 国产成人免费网站| 亚洲a∨无码精品色午夜| 91免费精品国自产拍在线不卡| 亚洲狠狠婷婷综合久久蜜芽| 日韩一卡2卡3卡4卡新区亚洲| 中文字幕免费在线| 亚洲爱情岛论坛永久| 本免费AV无码专区一区| 亚洲一区电影在线观看|