<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

    主站蜘蛛池模板: 亚洲国产成人一区二区精品区| 国产黄色片在线免费观看| 亚洲精品中文字幕无码蜜桃| www在线观看免费视频| 亚洲乱亚洲乱少妇无码| 国产vA免费精品高清在线观看| 亚洲精品和日本精品| 中文字幕免费在线观看动作大片| 久久久久亚洲精品无码网址| 日本免费高清视频| 亚洲最新在线视频| 日本免费中文字幕在线看| 野花视频在线官网免费1| 国产精品亚洲综合一区| 鲁丝片一区二区三区免费 | 国产L精品国产亚洲区久久| a视频在线免费观看| 亚洲日本一区二区| 国产在线国偷精品产拍免费| 香蕉97碰碰视频免费| 亚洲深深色噜噜狠狠爱网站| 99re免费99re在线视频手机版| 亚洲人成人77777网站不卡| 日韩黄色免费观看| 免费毛片在线看不用播放器| 亚洲精品午夜视频| 免费国产综合视频在线看| 中文字幕乱码一区二区免费| 亚洲人成网站看在线播放| 亚洲精品线路一在线观看| 日本免费一区二区三区四区五六区| 亚洲人成色777777精品| 亚洲午夜爱爱香蕉片| 成人在线免费看片| 一个人看的www免费高清| 久久久久亚洲av无码专区| 国产片免费在线观看| 色欲色香天天天综合网站免费| 亚洲av无码专区亚洲av不卡| 久久国产亚洲电影天堂| 日韩一级视频免费观看|