Eclipse RCP入门(四)
在RCP中使用spring
http://sourceforge.net/project/showfiles.php?group_id=145348
前人已经写了这个开源包了。下载过来。
把JAR包:
net.sourceforge.eclipsespring_0.1.0.jar
net.sourceforge.eclipsespring.example_0.1.0.jar
放在eclipse下面的plugins下面。嘿嘿。example其实可以不放。
打开plugin.xml文件
把如下内容加入
<extension
point="net.sourceforge.eclipsespring.beanFactoryContributors">
<beanFactoryContributor
class="com.sillycat.rcp.BeanFactoryContributor"
id="sillycatGen"/>
</extension>
扩展调用SPRING的XML配置文件
修改了VIEW的定义:
<view
category="sillycatGen"
class="net.sourceforge.eclipsespring.SpringExtensionFactory:sillycatGen/SampleView"
icon="icons/sample.gif"
id="com.sillycat.views.SampleView"
name="MainWindow">
</view>
注意其中的net.sourceforge.eclipsespring.SpringExtensionFactory:sillycatGen/SampleView中的
sillycatGen和最先定义的extension里面的ID保持一致
plugin.xml的全文如下:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension
point="net.sourceforge.eclipsespring.beanFactoryContributors">
<beanFactoryContributor
class="com.sillycat.rcp.BeanFactoryContributor"
id="sillycatGen"/>
</extension>
<extension
id="application"
point="org.eclipse.core.runtime.applications">
<application>
<run
class="com.sillycat.rcp.Application">
</run>
</application>
</extension>
<extension
point="org.eclipse.ui.perspectives">
<perspective
name="RCP Perspective"
class="com.sillycat.rcp.Perspective"
id="sillycatGen.perspective">
</perspective>
</extension>
<extension
id="product"
point="org.eclipse.core.runtime.products">
<product
application="sillycatGen.application"
name="sillycatGen">
<property
name="appName"
value="sillycatGen">
</property>
</product>
</extension>
<extension
point="org.eclipse.ui.views">
<view
category="sillycatGen"
class="net.sourceforge.eclipsespring.SpringExtensionFactory:sillycatGen/SampleView"
icon="icons/sample.gif"
id="com.sillycat.views.SampleView"
name="MainWindow">
</view>
</extension>
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension
targetID="org.eclipse.ui.resourcePerspective">
<view
id="com.sillycat.views.SampleView"
ratio="0.5"
relationship="right"
relative="org.eclipse.ui.views.TaskList">
</view>
</perspectiveExtension>
</extension>
</plugin>
在plugin.xml的Dependencies中加入:
net.sourceforge.eclipsespring
org.springframework
org.apache.commons.logging
在sillycatGen.product定义文件的Configuration中也加入:
net.sourceforge.eclipsespring
org.apache.commons.logging
org.springframework
另外在plugin.xml中的BeanFactoryContributor.java如下:
package com.sillycat.rcp;
import net.sourceforge.eclipsespring.util.AbstractBeanFactoryContributor;
public class BeanFactoryContributor extends AbstractBeanFactoryContributor {
protected String[] getResourcePaths() {
return new String[] { "/applicationContext.xml" };
}
}
spring的配置文件applicationContext.xml如下:
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userManager" class="com.sillycat.services.impl.UserManagerImpl"/>
<bean id="SampleView" singleton="false" class="com.sillycat.views.SampleView">
<property name="userManager">
<ref local="userManager"/>
</property>
</bean>
</beans>
userManager是个例子,UserManagerImpl.java如下:
package com.sillycat.services.impl;
import com.sillycat.services.UserManager;
public class UserManagerImpl implements UserManager {
public String getCurrentUserName() {
return System.getProperty("user.name");
}
}
在RCP中使用spring
http://sourceforge.net/project/showfiles.php?group_id=145348
前人已经写了这个开源包了。下载过来。
把JAR包:
net.sourceforge.eclipsespring_0.1.0.jar
net.sourceforge.eclipsespring.example_0.1.0.jar
放在eclipse下面的plugins下面。嘿嘿。example其实可以不放。
打开plugin.xml文件
把如下内容加入
<extension
point="net.sourceforge.eclipsespring.beanFactoryContributors">
<beanFactoryContributor
class="com.sillycat.rcp.BeanFactoryContributor"
id="sillycatGen"/>
</extension>
扩展调用SPRING的XML配置文件
修改了VIEW的定义:
<view
category="sillycatGen"
class="net.sourceforge.eclipsespring.SpringExtensionFactory:sillycatGen/SampleView"
icon="icons/sample.gif"
id="com.sillycat.views.SampleView"
name="MainWindow">
</view>
注意其中的net.sourceforge.eclipsespring.SpringExtensionFactory:sillycatGen/SampleView中的
sillycatGen和最先定义的extension里面的ID保持一致
plugin.xml的全文如下:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension
point="net.sourceforge.eclipsespring.beanFactoryContributors">
<beanFactoryContributor
class="com.sillycat.rcp.BeanFactoryContributor"
id="sillycatGen"/>
</extension>
<extension
id="application"
point="org.eclipse.core.runtime.applications">
<application>
<run
class="com.sillycat.rcp.Application">
</run>
</application>
</extension>
<extension
point="org.eclipse.ui.perspectives">
<perspective
name="RCP Perspective"
class="com.sillycat.rcp.Perspective"
id="sillycatGen.perspective">
</perspective>
</extension>
<extension
id="product"
point="org.eclipse.core.runtime.products">
<product
application="sillycatGen.application"
name="sillycatGen">
<property
name="appName"
value="sillycatGen">
</property>
</product>
</extension>
<extension
point="org.eclipse.ui.views">
<view
category="sillycatGen"
class="net.sourceforge.eclipsespring.SpringExtensionFactory:sillycatGen/SampleView"
icon="icons/sample.gif"
id="com.sillycat.views.SampleView"
name="MainWindow">
</view>
</extension>
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension
targetID="org.eclipse.ui.resourcePerspective">
<view
id="com.sillycat.views.SampleView"
ratio="0.5"
relationship="right"
relative="org.eclipse.ui.views.TaskList">
</view>
</perspectiveExtension>
</extension>
</plugin>
在plugin.xml的Dependencies中加入:
net.sourceforge.eclipsespring
org.springframework
org.apache.commons.logging
在sillycatGen.product定义文件的Configuration中也加入:
net.sourceforge.eclipsespring
org.apache.commons.logging
org.springframework
另外在plugin.xml中的BeanFactoryContributor.java如下:
package com.sillycat.rcp;
import net.sourceforge.eclipsespring.util.AbstractBeanFactoryContributor;
public class BeanFactoryContributor extends AbstractBeanFactoryContributor {
protected String[] getResourcePaths() {
return new String[] { "/applicationContext.xml" };
}
}
spring的配置文件applicationContext.xml如下:
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userManager" class="com.sillycat.services.impl.UserManagerImpl"/>
<bean id="SampleView" singleton="false" class="com.sillycat.views.SampleView">
<property name="userManager">
<ref local="userManager"/>
</property>
</bean>
</beans>
userManager是个例子,UserManagerImpl.java如下:
package com.sillycat.services.impl;
import com.sillycat.services.UserManager;
public class UserManagerImpl implements UserManager {
public String getCurrentUserName() {
return System.getProperty("user.name");
}
}