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

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

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

    隨筆-153  評論-235  文章-19  trackbacks-0
          當(dāng)為遺留系統(tǒng)加入spring時,經(jīng)典問題就是遺留系統(tǒng)需要引用spring管理的bean。幸好spring有機(jī)制可以處理這些。

    建一個類實現(xiàn)ApplicationContextAware接口,有一個引用ApplicationContext的靜態(tài)成員,然后,遺留系統(tǒng)需要引用spring管理的bean的地方,使用這個類。

    1.比如:我這里建一個SpringContext類

    package net.blogjava.chenlb;

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;

    /**
     * 此類可以取得Spring的上下文.
     * Spring 使new方法創(chuàng)建的對象可以引用spring管理的bean.
     * 2007-10-18 上午11:12:33
     * 
    @author chenlb
     
    */
    public class SpringContext implements ApplicationContextAware {

        
    protected static ApplicationContext context;
        
        
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            context 
    = applicationContext;
        }

        
    public static ApplicationContext getContext() {
            
    return context;
        }

    }

    2.然后在spring配置文件里加
    <bean id="springContext" class="net.blogjava.chenlb.SpringContext"></bean>

    3.其它類中引用
    MyBean myBean = (MyBean) SpringContext.getContext().getBean("myBean");

    4.如果老是寫SpringContext.getContext().getBean("...");麻煩,可以建一個工廠類來返回你要的bean
    package net.blogjava.chenlb;



    public class MyServerFactory {


        
    public static MyBean1 getMyBean1() {
            
    return (MyBean1) SpringContext.getContext().getBean("myBean1");
        }
        

    }


    ^_^
    posted on 2007-10-27 16:31 流浪汗 閱讀(15426) 評論(1)  編輯  收藏 所屬分類: Spring

    評論:
    # re: spring 依賴注入到直接new 對象[未登錄] 2011-05-17 17:20 | li
    正好解決了靜態(tài)類需要使用注入對象的問題  回復(fù)  更多評論
      
    主站蜘蛛池模板: 久久精品国产亚洲av麻豆小说| a毛片免费播放全部完整| 久久亚洲精品成人| 国产精品99久久免费| 免费看片在线观看| 91视频免费观看高清观看完整| 亚洲国产精品网站在线播放| 91在线亚洲精品专区| AV在线播放日韩亚洲欧| 国产精品黄页在线播放免费| 97视频免费在线| 免费无码VA一区二区三区| 国产精品无码免费专区午夜| 久久久久亚洲国产AV麻豆| 色偷偷女男人的天堂亚洲网 | 亚洲日韩AV一区二区三区中文| 亚洲国产精品国自产电影| 国产aⅴ无码专区亚洲av麻豆| 日本免费一区尤物| 久久久久国色AV免费观看性色| 99精品免费观看| 久久中文字幕免费视频| 日韩免费高清播放器| A毛片毛片看免费| 一个人看www免费高清字幕| 老司机精品视频免费| 青青青亚洲精品国产| 亚洲av无码有乱码在线观看| 亚洲xxxx18| 亚洲精华国产精华精华液网站 | 国产精品久久永久免费| 国产vA免费精品高清在线观看| 特级毛片A级毛片100免费播放| 国产AV无码专区亚洲AV蜜芽| jzzijzzij在线观看亚洲熟妇| 亚洲高清一区二区三区电影| 亚洲乱码日产精品一二三| 亚洲大码熟女在线观看| 豆国产96在线|亚洲| 一级毛片完整版免费播放一区| 一级一看免费完整版毛片|