现在的位置: 首页 > 综合 > 正文

在Eclipse RCP中使用Spring或者Hibernate

2013年09月28日 ⁄ 综合 ⁄ 共 3228字 ⁄ 字号 评论关闭
在RCP中使用Spring,最关键的一点在于spring配置文件的读取,因为RCP使用自己的ClassLoader,所以用通常的方法是无法装载Spring的配置文件。解决的思路是:在读取Spring配置文件时将RCP的ClassLoader暂时换一下。

在这里我根据Spring配置文件在项目中的存放位置,给出两种办法。

一、配置文件存放在源代码根目录下。

假设我有一个叫admin_console的项目,我把Spring的配置文件myspring.xml放在源代码根据目录src下,如下图所示
admin_console
      --src
           --cn      //包名
               --com
                  --chengang 
                      ---......     //源代码类
          --myspring.xml      //Spring配置文件,位于src目录下和cn目录平级
      --bin
      --lib
      --icons
      --properties

那么我们在RCP程序中可以这样来装载myspring.xml

        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        
try {
            Thread.currentThread().setContextClassLoader(this.getClass()
.getClassLoader());
            
ctx = new ClassPathXmlApplicationContext("/myspring.xml");
        } finally {
            Thread.currentThread().setContextClassLoader(oldLoader);
        }

二、配置文件存放在项目根目录的某个子目录下

项目根目录和源代码根目录是不同的两个概念。如上图的项目结构中,src是源代码根目录,admin_console是项目根目录,那么properties就是项目根目录下的一个子目录。

如果将myspring.xml放入到properties目录中,以上的读取代码就没用了,读取方法如下:

        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        
try {
            Thread.currentThread().setContextClassLoader(this.getClass()
.getClassLoader());
   ctx = new FileSystemXmlApplicationContext(ProjectUtil.toFullPath("properties/myspring.xml"));
        } finally {
            Thread.currentThread().setContextClassLoader(oldLoader);
        }

其中ProjectUtil.toFullPath是我自己写的一个方法,主要是得到myspring.xml的绝对路径,其代码如下:

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
 
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.plugin.AbstractUIPlugin;
 
import com.wxxr.management.admin.console.AdminConsolePlugin;
 
/**
 * 用于插件项目和非插件项目,提供两者通用的方法接口
 * @author chengang 2006-3-30
 */
public class ProjectUtil {
 
    private static AbstractUIPlugin plugin = AdminConsolePlugin.getDefault();
 
    private ProjectUtil() {}
 
    /**
     * 判断当前的运行状态是否为插件方式
     * @return true=插件方式运行
     */
    private static boolean isPlugin() {
        return plugin != null;
    }
 
    public static URL getURL(String path) {
        if (isPlugin())//如果是插件
            return FileLocator.find(plugin.getBundle(), new Path(path), null);
        else
            try {
                return new URL("file:" + path);
            } catch (MalformedURLException e) {
                throw new RuntimeException(path + " is error", e);
            }
    }
 
    public static InputStream getInputStream(String path) {
        URL url = getURL(path);
        try {
            return url.openStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static String toFullPath(String path) {
        if (isPlugin()) {
            try {
                return FileLocator.toFileURL(ProjectUtil.getURL(path)).getPath();
            } catch (IOException e) {
                throw new RuntimeException(path + " toFullPath is fault", e);
            }
        } else {
            return path;
        }
    }
 
}

三、总结

上面两种方式那一种更好呢?应该是第二种。一般来说,源代码的编译文件会打成一个jar包(其实不打成一个JAR包也可以的,我在很多以前就尝试过将class文件松散的部署,如果哪个类要修改,修改后就只部署覆盖这个class,看起来也挺方便。不过这种方式不是最佳实践,不推荐正式发布时使用,一不心可能引起依赖它的其他类出现问题。)。如果用第一种方式在项目打包后,myspring.xml会打包到jar文件中,这样不利于今后对myspring进行动态修改。如果用第二种就没有这种缺点。

很多时候,在Eclipse开发环境中,运行RCP程序没有问题。但导出项目后,在独立的环境中却报配置文件(不光是Spring)找不到的错误,解决的方法都基本与此相同。

抱歉!评论已关闭.