Eclipse RCP + Spring with Hibernate flavor

来源:互联网 发布:河北三金网络怎么样 编辑:程序博客网 时间:2024/06/07 20:09
如果你曾考虑如何将Spring和你的Eclipse RCP 应用集成,那么希望这篇能对你有帮助。

本文翻译自 Eclipse RCP + Spring with Hibernate flavor 一文,文章版权归原作者所有

Image本文来自: Alpha Atom 

Image译者: Samuel Chen

 

public class MyPlugin extends AbstractUIPlugin {



private ApplicationContext applicationContext;



private static final String[] SPRING_CONFIGS = {

"/applicationContext.xml",

"/applicationContextDatabase.xml"

};



public void start(BundleContext context) throws Exception {

super.start(context);



ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();

try {

Thread.currentThread().setContextClassLoader(

this.getClass().getClassLoader());

applicationContext = new ClassPathXmlApplicationContext(SPRING_CONFIGS);

} finally {

Thread.currentThread().setContextClassLoader(oldLoader);

}

}



public ApplicationContext getApplicationContext() {

return applicationContext;

}



/* ... */

}


然后,你可以简单的通过调用得到Spring context。

MyPlugin.getDefault().getApplicationContext()

当然,设定我们自己的classloader不是太好,但是至少会没有问题,例如,Hibernate。那么我们现在定义一些beans:

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>system.properties</value>

</list>

</property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate.LocalSessionFactoryBean"

lazy-init="true">

<property name="mappingJarLocations">

<list>

<value>${hibernate.hbms}</value>

</list>

</property>

</bean>

Now, if you run your application as standalone RCP (via eclipse.exe) then set ${hibernate.hbms} property like this:

现在,如果你将你的应用作为单独的RCP(通过 eclipse.exe)运行,那么就象下面这样设定${hibernate.hbms}

hibernate.hbms = file:plugins/plugin_1.0.0/project-hbm.jar

 路径应该是和你的RCP应用目录的相对路径。如果你计划在Eclipse里面测试你的应用,那么${hibernate.hbms}应该被设置成绝对路径,就像这样:

hibernate.hbms = file:/work/project/lib/project-hbm.jar

在Eclipse 中运行你的应用需要确定,Spring的配置文件在你项目的classpath中。如果你不想将他们和你的java类放到一起(就像我一样),你可以选择 “Allow output folders for source folders"(项目属性,Java编译路径标签),将目录添加到设置并且将其输出目录设定为自身(如果有时你计划清除你的workspace,这将会有一点危险。 :-) )最后一件要记住的事是在你项目属性的“Self-Hosting"标签中打开这个最新添加的目录。

如果你知道更好的方法来集成Spring与Eclipse RCP请告诉我。