<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, 評論 - 4, 引用 - 0
    數據加載中……

    《Pro Spring》學習筆記(9)--基礎進階(4)

    ?

    ????? 到目前為止,我們都是使用BeanFactory接口以及它的子接口來使用Spring,盡管使用BeanFactory也是一種不錯的方式,但正如前面看到的,有些時候,我們還是不得不自己編寫代碼來調用諸如preInstantiateSingletons,postProcessBeanFactory等方法。
    ????? 為了使用的方便,Spring提供了BeanFactory的一個擴展:ApplicationContext。使用ApplicationContext,我們可以減少需要編寫的代碼的數量,并且ApplicationContext也增加了一些新的功能。如果在Web工程中使用Spring,我們甚至可以讓Spring自動加載ApplicationContext,而無需自己編寫代碼來創建它。
    ????? ApplicationContext具備一些BeanFactory不具備的功能:國際化(Internationalization),事件發布(Event publication),資源管理和訪問(Resource management and access),更多的生命周期控制接口(Additional lifecycle interfaces)以及底層組件的自動配置(Improved automatic configuration of infrastructure components)。
    ????? Spring為ApplicationContext接口提供了三種實現:FileSystemXmlApplicationContext,ClasspathXmlApplicationContext和XmlWebApplicationContext。其中XmlWebApplicationContext是專為Web工程定制的,并且我們可以使用ContextLoaderListener或ContextLoaderServlet來自動加載ApplicationContext配置。
    ????? 通常國際化是通過使用MessageSource接口來實現的,為了在ApplicationContext中使用MessageSource,我們需要配置一個類型為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方法來獲取資源內容了。
    ????? ApplicationContext的另一個重要特性就是對發布和接收事件的支持,ApplicationContext會在其被配置的時候自動注冊所有的偵聽器(listener),事件的發布是通過ApplicationContext的publishEvent方法來實現的。
    //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>
    ????? 資源訪問是我們經常碰到的,這些資源有可能存在于一個文件中,類路徑中的一個jar包中,抑或是在遠程服務器上。Spring為我們提供了統一的,協議無關的方式來訪問各種各樣的資源,這給我們訪問各種資源提供了極大的方便。
    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) 評論(0)  編輯  收藏 所屬分類: spring

    主站蜘蛛池模板: 国产免费爽爽视频在线观看| 曰批全过程免费视频免费看| 青青青国产免费一夜七次郎| 一级女性全黄久久生活片免费| 男女啪啪永久免费观看网站| 男女猛烈无遮掩视频免费软件| 午夜寂寞在线一级观看免费| 久久国产精品免费一区| 亚洲精品无码久久久久久久| 四虎亚洲国产成人久久精品| 国产精品网站在线观看免费传媒| 亚洲熟妇丰满多毛XXXX| 久久久免费观成人影院| 亚洲人色大成年网站在线观看| 啦啦啦高清视频在线观看免费 | 免费va人成视频网站全| 一区二区三区四区免费视频 | 国产精品美女久久久免费| 亚洲欧洲日产国码一级毛片 | 在线观看免费成人| 日本亚洲欧美色视频在线播放| 国产又长又粗又爽免费视频| 久久久国产精品无码免费专区| 亚洲国产日韩一区高清在线 | 亚洲AV无码久久精品成人| 好爽好紧好大的免费视频国产| 国产精品亚洲一区二区在线观看 | 亚洲乱码日产精品a级毛片久久| 一级毛片免费毛片毛片| 亚洲精品无码专区久久久| 国产精品深夜福利免费观看 | 在线天堂免费观看.WWW| 亚洲a∨无码一区二区| 亚洲综合一区二区精品久久| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲伦理一区二区| 五月婷婷亚洲综合| 人人玩人人添人人澡免费| 一区二区三区免费视频观看| 亚洲AV无码一区东京热| 久久久久亚洲av成人无码电影|