解决了RCP打包时Spring配置的问题

转载 在Eclipse RCP中使用Spring或者Hibernate收藏
http://blog.csdn.net/ttodle/archive/2006/04/27/679422.aspx


新一篇: JAVA实现用系统关联的程序打开文件 | 旧一篇: 在 Ubuntu 5.10 上安裝 Sun Java SDK 1.5
在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)找不到的错误,解决的方法都基本与此相同。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值