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

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

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

    guangnian0412's BLOG

    Java in my life

    常用鏈接

    統計

    積分與排名

    我關注的Blog

    最新評論

    Eclipse RCP與Spring的整合

    ??? Eclipse RCP與Spring的整合

    ??? 最近上一個項目想在Eclipse RCP中使用Spring,在網上Google了一下發現這方面的資料比較少,知道Spring自己有個Spring-OSGI的項目,可以在Spring中配置OSGI服務。可是,我只是想在RCP中引入Spring來管理Java Bean,不想去研究那個東西。于是,看看有沒有什么簡單的方法來解決這個問題。在陳剛的BlOG中找到了問題的部分答案。
    ??????
    ??????? 于是,我在RCP項目的activator class中加入了

    ?1 ? ? private?ApplicationContext?ctx;
    ?2?
    ?3???? private?void?initializeApplicationContext()?{
    ?4?????????ClassLoader?oldLoader?=?Thread.currentThread().getContextClassLoader();
    ?5?????????try{
    ?6?????????????Thread.currentThread().setContextClassLoader(getDefault().getClass().getClassLoader());
    ?7?????????????this.ctx?=?new?FileSystemXmlApplicationContext(ProjectUtil.toFullPath("properties/applicationContext.xml"));
    ?8?????????}?finally?{
    ?9?????????????Thread.currentThread().setContextClassLoader(oldLoader);
    10?????????}
    11?????}

    ProjectUtil.toFullPath()方法在陳剛的BLOG中有詳細的說明,是一個獲得項目絕對路徑的方法。另外在陳剛的BLOG中提到了,在Eclipse 3.2M6中已經不需要轉換ClassLoader。但是,我用的是3.2 release版,還是需要轉換ClassLoader才能正常工作啊。覺得這并不像陳剛所說的BUG,Eclipse的每個Plugin都有自己的ClassLoader,所以需要轉換吧。

    ??? 然后,在start方法中調用initializeApplicationContext方法,并為
    ctx提供getter

    1?????public?void?start(BundleContext?context)?throws?Exception?{
    2?????????super.start(context);
    3?????????initializeApplicationContext();
    4?????}
    5?
    6?????public?ApplicationContext?getApplicationContext()?{
    7?????????return?this.ctx;
    8?????}

    這樣我們在其他地方就可以用
    Activator.getDefault().getApplicationContext()得到ApplicationContext了。

    ??????? 但是,新的問題又來了,如何把RCP中的組件也納入Spring的管理呢,比如ViewPart。我又Google了一下,在今年的TSE2006上有一場報告就提到了Spring同Eclipse RCP的整合 ,里面提到了利用Eclipse的? ?? IExecutableExtensionFactory和IExecutableExtension接口,確實非常的簡單。
    ? ? ? ? 通常,我們自己定義的ViewPart是通過擴展點
    org.eclipse.ui.views,由Eclipse的Workbench自動創建,像這樣:

    <extension?point="org.eclipse.ui.views">
    <view
    ????????? name="myView"
    ????????? class
    ="org.eclipse.example.rcpspring.MyView"
    ????????? id
    ="org.eclipse.example.rcpspring.view">
    </view>
    </extension>
    ?????
    ?????? 現在我們通過Spring來管理這個view,并假設為其注入一個businessService Bean,像這樣:

    <bean?id="myView" class="org.eclipse.example.rcpspring.MyView">
    <property?name="businessService" ref="businessService"/>
    </bean>

    ?????? 然后,我們要創建一個Extension Factory來在RCP中注冊這個view,代碼如下:

    ?1?public?class?MyViewExtensionFactory?implements?IExecutableExtensionFactory,
    ?2?????????IExecutableExtension?{
    ?3?????private?ViewPart?view;
    ?4?
    ?5?????public?Object?create()?throws?CoreException?{
    ?6?????????return?this.view;
    ?7?????}
    ?8?
    ?9?????public?void?setInitializationData(IConfigurationElement?config,
    10?????????????String?propertyName,?Object?data)?throws?CoreException?{
    11?????????this.view?=?(MyView)Activator.getDefault().getApplicationContext().getBean("myView");
    12?????????this.view.setInitializationData(config,?propertyName,?data);
    13?????}
    14?}

    通過
    Activator.getDefault().getApplicationContext()來取出上面建立的ApplicationContext。

    ????? 最后,我們要用這個
    MyViewExtensionFactory來注冊擴展點,如下:

    <extension?point="org.eclipse.ui.views">
    <view
    name="myView"
    class
    ="org.eclipse.example.rcpspring.MyViewExtensionFactory"
    id
    ="org.eclipse.example.rcpspring.view">
    </view>
    </extension>

    MyViewExtensionFactory 來取代原來的MyView

    ? ? ?? 好,已經大功告成。MyView已經成功的進入了Spring框架的管理。其他的RCP擴展點也可以如此炮制。 ???????????

    posted on 2006-12-30 21:11 guangnian 閱讀(5595) 評論(5)  編輯  收藏 所屬分類: JAVA其他

    評論

    # re: Eclipse RCP與Spring的整合 2008-06-19 15:24 win

    想請教一下:

    "ProjectUtil.toFullPath()方法在陳剛的BLOG中有詳細的說明,是一個獲得項目絕對路徑的方法。"

    ----->ProjectUtil中的AdminConsolePlugin是什么?是他自己寫的類,還是公用開源包中的類?
    我不理解這兩個地方:
    1>import com.wxxr.management.admin.console.AdminConsolePlugin;
    2> private static AbstractUIPlugin plugin = AdminConsolePlugin.getDefault();

    所以我用不了這種方法。
    我的目的,是想在rcp中使用spring,hibernate.
    我在application方式下,可以成功用hibernate從數據庫中取值,但是,一放入rcp中的button中,就失敗。
    所以,看了你的文章以及陳剛的文章,但是,不明白以上的兩個地方,是什么?
    不管有沒有回復,都感謝:)
    ----------------------------------------------
    .......
    import com.wxxr.management.admin.console.AdminConsolePlugin;

    /**
    * 用于插件項目和非插件項目,提供兩者通用的方法接口
    * @author chengang 2006-3-30
    */
    public class ProjectUtil {

    private static AbstractUIPlugin plugin = AdminConsolePlugin.getDefault();

    private ProjectUtil() {}
    .....  回復  更多評論   

    # re: Eclipse RCP與Spring的整合 2008-06-19 15:25 win

    我現在的問題,是在rcp中取ctx,失敗。  回復  更多評論   

    # re: Eclipse RCP與Spring的整合 2008-08-31 23:38 zwy

    同樓上,我在測試main中可以利用ApplicationContext,但在RCP View中卻不能使用,總是報錯:Error creating the view.Reason:org/springframework/context/ApplicationContext.

    public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("invoker-client.xml");
    IWarrantService service = (IWarrantService)context.getBean("warrantServiceProxy");
    warrantData.setContext(context);
    warrantData.setService(service);

    System.out.println("Oracle數據庫中共有"+ warrantData.getAllWarrants().size()+"條記錄");
    }  回復  更多評論   

    # re: Eclipse RCP與Spring的整合 2008-08-31 23:39 zwy

    請達人賜教,謝謝!  回復  更多評論   

    # re: Eclipse RCP與Spring的整合 2009-03-23 21:54 dcb

    http://dcbjavaeye.javaeye.com/admin/blogs/351449  回復  更多評論   

    主站蜘蛛池模板: 777亚洲精品乱码久久久久久| 亚洲成AV人在线观看网址| 国产A在亚洲线播放| 一级全免费视频播放| 亚洲国产aⅴ综合网| 爱情岛论坛亚洲品质自拍视频网站| 中文字幕av无码无卡免费| 亚洲av片不卡无码久久| 亚洲成在人线aⅴ免费毛片| 亚洲mv国产精品mv日本mv| 97无码免费人妻超级碰碰碰碰| 亚洲日本视频在线观看| 免费毛片a在线观看67194| 亚洲国产精品免费观看| 免费的一级片网站| 一区二区三区免费视频观看| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 免费乱理伦在线播放| 国产91成人精品亚洲精品| 亚洲精品麻豆av| 高清一区二区三区免费视频 | 亚洲国产成人久久精品app| 最近中文字幕mv免费高清电影| 色天使亚洲综合在线观看| 国产免费小视频在线观看| 国产在线观看免费av站| 亚洲成人午夜在线| 免费无码AV电影在线观看| 免费在线人人电影网| 亚洲国产精品无码AAA片| 青娱分类视频精品免费2| 春暖花开亚洲性无区一区二区| 亚洲中文字幕在线第六区| 无码国产精品一区二区免费式芒果| 亚洲国产精品xo在线观看| 免费中文字幕在线| 日本人的色道免费网站| 男女猛烈激情xx00免费视频| 亚洲欧洲第一a在线观看| 日韩免费福利视频| 久久青草免费91观看|