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 阅读(5480) 评论(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  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: