本文介绍如何利用eclipse插件spring ide在eclipse中的使用。
eclipse是目前非常流行的开发平台,开放扩展的架构让很多程序员找到了自己个性化的工作环境。
问题提出:
在采用spring框架进行系统开发时,bean的配置文件xml的书写、xml文件的内容校验及bean之间的依赖关系查看等工作,如果能够采用合适的工具来完成,会成倍提高开发效率。
解决方法:
spring ide是spring官方网站推荐的eclipse插件,可提供在开发spring时对bean定义文件进行验证并以可视化的方式查看各个bean之间的依赖关系等。
同时,建议结合eclipse xmlbuddy插件进行xml文件编辑与校验,是个不错的选择。
安装说明
jdk:1.5.0 从http://java.sun.com上去下载安装
eclipse:3.1.2 从http://www.eclipse.org 上去下载解压
spring 1.2.8 从http://www.springframework.org 上去下载解压
spring ide 1.3.2 从http://springide.org 上去下载
xmlbuddy 2.0.72 从http://www.xmlbuddy.com/ 上去下载
spring ide 1.3.2 update地址为:http://springide.org/updatesite/
管理技巧
提示:新下载的插件plugin一定不要都放在原始的eclipse目录下去,一大堆,累死你:(
- 前提是你已经下载解压并设置好eclipse工具,比如解压在e:/opensource/eclipse/目录下,以下这个目录以%eclipse_home%来进行表示;
- 此时默认的插件是在%eclipse_home%/plugins目录中的;
- 在%eclipse_home%下建立一个pluginsnew的目录;
比如:e:/opensource/eclipse/pluginsnew - 如果你下载了一个新的插件,比如叫做:xyz
那么就在%eclipse_home%/pluginsnew/目录下建立xyz目录,目录里面是eclipse目录,eclipse目录包含有features与plugins两个子目录;结构如下图所示:
- 把下载的新插件的文件放在以下相应目录中;
%eclipse_home%/pluginsnew/xyz/eclipse/features
%eclipse_home%/pluginsnew/ xyz/eclipse/plugins - 建立相关的.link的文件;
然后在%eclipse_home%/links目录里建立一个xyz.link的文件
内容如是:
path=e:/opensource/eclipse/pluginsnew/xyz
就一行这样的路径指示而已。
这样,如果你下载了多个插件就可以如法炮制建立多个link文件,想加载哪个插件就把哪个插件的link文件放到%eclipse_home%/links的目录中即可,使用与管理都很方便,建议千万不要放在默认的安装目录中,这样对于升级eclipse主程序也方便一些;当然如果你喜欢用find and install…进行安装的话也可以的;
如果上面你的%eclipse_home%与此不同,请修改xyz.link文件里的路径。 - 删除插件,先关闭eclipse;
删除%eclipse_home%/links/xyz.link文件即可
删除%eclipse_home%/pluginsnew/xyz整个目录及文件 - 重新启动eclipse,这样就可以了。如果插件没有生效或者没有删除,请加上-clean进行启动eclipse,即eclipse.exe ?cclean
- 插件安装验证及记录详见:eclipse的workspace下面的.metadata/.log文件,比如:%eclipse_home%/workspace/.metadata/.log文件,有问题的话,打开这个文件看看并进行解决。
此插件安装方法采用上一节的《eclipse使用技巧之插件管理》
重新启动eclipse后,在help è about eclipse sdk è plug-in details你可以看到由“spring ide developer”提供的“spring ide”版本为“1.3.2”插件及相关的graph、ui及ui search插件,如下图所示:

相关要求
注意:为了让spring ide插件能够显示可视化的bean结构图形及bean之间的依赖关系,需要eclipse gef(graphical editing framework)的支持,自己下载并解压安装,安装技巧同上《eclipse使用技巧之插件管理》。
spring 1.2系列当前最新的稳定版本为1.2.8,下载时可以选择包含spring所依赖的一些开源包的文件spring-framework-1.2.8-with-dependencies.zip,如果你已经清楚并有了spring所依赖的相关开源包,就单独下载spring-framework-1.2.8.zip即可。建议下载前者。
解压后目录结构及包的说明如下:

dist目录下是spring的发布包,关于发布包下面会详细进行说明;
docs目录下是相关的文档,包括有spring api的javadoc、reference参考指南、spring的taglib标签使用文件及spring mvc的mvc-step-by-step讲解与示例;
lib目录下是spring所依赖的第三方开源包;
mock目录下是spring辅助应用测试的mock源程序;
samples目录下是spring的示例源程序及简单的webapp示例框架的示例配置,值得好好学习的有jpetstore及petclinic,当然其它的countries、imagedb、tiles-example也可以好好参考一下;
src目录下是spring的源程序;
test目录下spring的单元测试源程序;
tiger目录下是java 1.5 tiger方面的相关及测试源程序。
spring包结构说明
接下来详细说说dist目录下jar包的相关内容
spring.jar是包含有完整发布的单个jar包,spring.jar中除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。
除了spring.jar文件,spring还包括有其它13个独立的jar包,各自包含着对应的spring组件,用户可以根据自己的需要来选择组合自己的jar包,而不必引入整个spring.jar的所有类文件。
spring-core.jar
这个jar文件包含spring框架基本的核心工具类,spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。
spring-beans.jar
这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行inversion of control / dependency injection(ioc/di)操作相关的所有类。如果应用只需基本的ioc/di支持,引入spring-core.jar及spring-beans.jar文件就可以了。
spring-aop.jar
这个jar文件包含在应用中使用spring的aop特性时所需的类。使用基于aop的spring特性,如声明型事务管理(declarative transaction management),也要在应用里包含这个jar包。
spring-context.jar
这个jar文件为spring核心提供了大量扩展。可以找到使用spring applicationcontext特性时所需的全部类,jdni所需的全部类,ui方面的用来与模板(templating)引擎如velocity、freemarker、jasperreports集成的类,以及校验validation方面的相关类。
spring-dao.jar
这个jar文件包含spring dao、spring transaction进行数据访问的所有类。为了使用声明型事务支持,还需在自己的应用里包含spring-aop.jar。
spring-hibernate.jar
这个jar文件包含spring对hibernate 2及hibernate 3进行封装的所有类。
spring-jdbc.jar
这个jar文件包含对spring对jdbc数据访问进行封装的所有类。
spring-orm.jar
这个jar文件包含spring对dao特性集进行了扩展,使其支持 ibatis、jdo、ojb、toplink,因为hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖spring-dao.jar里的类,用这个包时你需要同时包含spring-dao.jar包。
spring-remoting.jar
这个jar文件包含支持ejb、jms、远程调用remoting(rmi、hessian、burlap、http invoker、jax-rpc)方面的类。
spring-support.jar
这个jar文件包含支持缓存cache(ehcache)、jca、jmx、邮件服务(java mail、cos mail)、任务计划scheduling(timer、quartz)方面的类。
spring-web.jar
这个jar文件包含web应用开发时,用到spring框架时所需的核心类,包括自动载入webapplicationcontext特性的类、struts与jsf集成类、文件上传的支持类、filter类和大量工具辅助类。
spring-webmvc.jar
这个jar文件包含spring mvc框架相关的所有类。包含国际化、标签、theme、视图展现的freemarker、jasperreports、tiles、velocity、xslt相关类。当然,如果你的应用使用了独立的mvc框架,则无需这个jar文件里的任何类。
spring-mock.jar
这个jar文件包含spring一整套mock类来辅助应用的测试。spring测试套件使用了其中大量mock类,这样测试就更加简单。模拟httpservletrequest和httpservletresponse类在web应用单元测试是很方便的。
如何选择这些发布包,决定选用哪些发布包其实相当简单。如果你正在构建web应用并将全程使用spring,那么最好就使用单个全部的spring.jar文件;如果你的应用仅仅用到简单的inversion of control / dependency injection(ioc/di)容器,那么只需spring-core.jar与spring-beans.jar即可;如果你对发布的大小要求很高,那么就得精挑细选了,只取包含自己所需特性的jar文件了。采用独立的发布包你可以避免包含自己的应用不需要的全部类。当然你可以采用其它的一些工具来设法令整个应用包变小,节省空间的重点在于准确地找出自己所需的spring依赖类,然后合并所需的类与包就可以了。eclispe有个插件叫classpath helper可以帮你找找所依赖的类。
spring包依赖说明
spring-core.jar需commons-collections.jar,spring-core.jar是以下其它各个的基本。 spring-beans.jar需spring-core.jar/cglib-nodep-2.1_3.jar spring-aop.jar需spring-core.jar/spring-beans.jar/cglib-nodep-2.1_3.jar/aopalliance.jar spring-context.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/commons-collections.jar/aopalliance.jar spring-dao.jar需spring-core.jar/spring-beans.jar/spring-aop.jar/spring-context.jar spring-jdbc.jar需spring-core.jar/spring-beans.jar/spring-dao.jar spring-web.jar需spring-core.jar/spring-beans.jar/spring-context.jar spring-webmvc.jar需spring-core.jar/spring-beans.jar/spring-context.jar/spring-web.jar spring-hibernate.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-jdbc.jar/spring-orm.jar/spring-web.jar/spring-webmvc.jar spring-orm.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-jdbc.jar/spring-web.jar/spring-webmvc.jar spring-remoting.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-context.jar/spring-web.jar/spring-webmvc.jar spring-support.jar需 spring-core.jar/spring-beans.jar/spring-aop.jar/spring-dao.jar/spring-context.jar/spring-jdbc.jar spring-mock.jar需spring-core.jar/spring-beans.jar/spring-dao.jar/spring-context.jar/spring-jdbc.jar插件介绍
我们简单地创建一个示例工程,里面包含有一个接口类与两个实现该接口的实现类进行演示说明使用的方法。
需要引入spring.jar、commons-logging.jar、log4j.jar
单个引入需要引入spring-core.jar、spring-beans.jar、spring-context.jar
接口类:ihelloworld.java
public interface ihelloworld { string sayhelloworld(); }实现类一:helloworld1.java
public class helloworld1 implements ihelloworld { public helloworld1() { super(); } public string sayhelloworld() { return "hello world helloworld1"; } } 实现类二:helloworld2.java
public class helloworld2 implements ihelloworld { public helloworld2() { super(); } public string sayhelloworld() { return "hello world helloworld2"; } } 根据常用的三层与spring的最佳实践,将配置文件分成了四个
beanreffactory.xml负责总装,由singletonbeanfactorylocator来装入
通过classpathxmlapplicationcontext来把其它三个模块的文件引入
beanrefdataaccess.xml负责dao,与数据库相关的bean都定义在这里
beanrefservice.xml负责service层的bean定义
beanrefmvc.xml负责spring mvc方面相关的bean定义等等
以下配置文件的bean定义为演示所用,各自的内容如下:
beanreffactory.xml的内容如下:
<xml version="1.0" encoding="utf-8"> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="beanfactory" class="org.springframework.context.support.classpathxmlapplicationcontext"> <constructor-arg> <list> <value>beanrefdataaccess.xml</value> <value>beanrefservice.xml</value> <value>beanrefmvc.xml</value> </list> </constructor-arg> </bean> </beans>
beanrefdataaccess.xml的内容如下:
<xml version="1.0" encoding="utf-8"> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloworlddao1" class="helloworld1"/> <bean id="helloworlddao2" class="helloworld2"/> </beans>
beanrefservice.xml的内容如下:
<xml version="1.0" encoding="utf-8"> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloworld1" class="helloworld1"/> <bean id="helloworld2" class="helloworld2"/> <bean id="springdemoconstructor" class="springdemoconstructor"> <constructor-arg> <value>spring ide constructor</value> </constructor-arg> <property name="helloworld"> <ref bean="helloworld1"></ref> </property> </bean> <bean id="springdemosetter" class="springdemosetter"> <property name="hello" value="spring ide setter"/> <property name="helloworld"> <ref bean="helloworld2"></ref> </property> </bean> </beans>
beanrefmvc.xml的内容如下:
<xml version="1.0" encoding="utf-8"> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloworldmvc1" class="helloworld1"/> <bean id="helloworldmvc2" class="helloworld2"/> </beans>
右键项目工程:可以看到“add spring project nature”菜单,单击它

细心的你一定可以看到项目工程springidedemo前面的图标变成了一个s的标志,而不是原来的j,当然项目里与spring相关的文件都会在图标上显示一个s标志。j
右键弹出的菜单里就会有个“remove spring project nature”的菜单了。
你选择这个菜单进行移除后就不能进行以下的相关操作了。

window è show view è other… è 可以看到spring ide,打开它,如下所示,所中它并确定。

可以看到如下的视图,右键工程的名称,在弹出的properties菜单上点击它

弹出属性框,让我们来添加配置文件

确定后,可以看到刚才选择的四个配置文件已经在里面了,再次确定。

在spring beans视图里展开工程后,便可以看到增加进来的四个配置文件了。

也可以将配置文件放到配置集合里,如下所示:

这样确定以后,可以看到四个文件都属于service的节点下面,如下所示。

再次确定后,在spring beans视图里看到service节点,展开它,可以看到所有配置文件里的bean都在这里列出来了。

展开显示两个定义的bean结点,右键其中的一个结点,弹出四个菜点,最后一个就是上面的配置菜单,不再讲解了,我们选中“open config file”菜单

于是就会自动打开配置文件,并定位到bean的定义位置上面去了,如下所示,方便进行查看与改动。

在右键弹出的菜单点选中“open bean class”,

就会自动定位到对应的类文件里去了,如下所示。

在右键弹出的菜单点选中“show graph”,

就会把当前这个bean以图形的方式显示出来,如下所示。

但是这个演示的文件仅是一个独立的bean,没有任何关联等,下面会演示关联等。
在spring beans视图里展开工程后,选中src/beanrefserice.xml配置文件,在右键弹出的菜单点选中“show graph”

就会把当前整个配置文件的内容以bean图形的方式显示出来,如下所示。
以下演示bean的引用及构造注入与setter注入何关联等,更多的操作类似了。
定义一个接口类ispringdemo.java,有两个方法
public interface ispringdemo { ihelloworld gethelloworld(); string gethello(); } 实现类一springdemoconstructor.java,含有构造注入及setter注入
public class springdemoconstructor implements ispringdemo { private string hello; private ihelloworld helloworld; public springdemoconstructor(string hello) { this.hello = hello; } public string gethello() { return hello; } public ihelloworld gethelloworld() { return helloworld; } public void sethelloworld(ihelloworld helloworld) { this.helloworld = helloworld; } } 实现类二springdemosetter.java,都是setter注入
public class springdemosetter implements ispringdemo { private string hello; private ihelloworld helloworld; public string gethello() { return hello; } public void sethello(string hello) { this.hello = hello; } public ihelloworld gethelloworld() { return helloworld; } public void sethelloworld(ihelloworld helloworld) { this.helloworld = helloworld; } } 配置文件beanrefservice.xml增加bean定义,成为如下,红字为新增的
<xml version="1.0" encoding="utf-8"> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloworld1" class="helloworld1"/> <bean id="helloworld2" class="helloworld2"/> <bean id="springdemoconstructor" class="springdemoconstructor"> <constructor-arg> <value>spring ide constructor</value> </constructor-arg> <property name="helloworld"> <ref bean="helloworld1"></ref> </property> </bean> <bean id="springdemosetter" class="springdemosetter"> <property name="hello" value="spring ide setter"/> <property name="helloworld"> <ref bean="helloworld2"></ref> </property> </bean> </beans>
注意:
如果bean是定义在同一个文件里的可以用
<ref local="helloworld2"></ref>
如果不是在同一个配置文件里的,要用
<ref bean="helloworld2"></ref>
要不然会报到bean找不到!
这样配置完成后,我们切换到“spring beans”视图,在beanrefservice.xml节点上右键打开“show graph”菜单,可以看到如下所示:

这时bean里面简单的引用关系就表现出来了。

在bean图上右键,会打开三个操作菜单:
open java type即定位到bean的类文件上面;
open config file即定位到bean的xml配置文件上面;
show in beans view即定位到“spring beans”的视图里的具体某个节点上,如下所示。

此时我们可以在配置集合的service节点上面右键,选择“show graph”

这样就会显示出所有的配置文件集合里的所有bean文件及其关联引用情况,如下图。

带圈的p图标表示是属性,即setter方法注入,带圈的c图标表示构造注入。箭头表示引用关系了。j
中间一排演示用的四个bean没有关联引入,就光杆司令地在那里排队了啦j
最下面的带圈的c后面又跟着一大排参数,表示集合类型的了。如list
spring ide提供错误检查功能,比如我在配置文件中输入一个不存在的类的名称,保存后,就会在边上出现红色提示,鼠标移上去就会出现提示信息,如下图所示。

当然也会有problems视图里显示相应的错误信息,如下图:
xmlbuddy由于打开xml文件时会根据xml文件里定义的dtd去网上查找相应的dtd文件,这样导至打开的时候会相当慢,特别是如果你的机器上不了外网的话,就更慢了。
解决的办法有两个:
一、在本机开启web服务(如tomcat等等),并设置端口为80,然后根据dtd 的目录结构建立相应的结构,并把dtd文件放进去。然后在hosts文件里把本机的地址映射到相应的dtd指定的网站去,如:
127.0.0.1// www.springframework.org
这样打开xml文件时就会在本机寻找dtd文件进行验证,速度就快很多了j
二、打开xmlbuddy插件的安装目录,并找到其中的cache目录,比如:%eclipse_home%/pluginsnew/xmlbuddy/eclipse/plugins/com.objfac.xmleditor_2.0.72/.cache
打开meta.xml文件,一看就知道怎么处理了吧,如法炮制,选把dtd文件拷贝到这个cache目录里,再增加内容到meta.xml里来,比如:
<file> <pub>-//spring//dtd bean//en</pub> <abs>http://www.springframework.org/dtd/spring-beans.dtd</abs> <rel>spring-beans.dtd</rel> </file>
要增加其它的dtd文件方法类似,不断地增加说明与dtd文件即可。
于是在编辑xml文件时就会有相关的提示及错误提示信息,可以减少一些书写xml文件时发生的低级错误了。

测试与运行
先建立一个log4j的配置文件log4j.properties,内容如下
# set root logger level to error and its only appender to r. log4j.rootlogger = info,r # r is set to be a dailyrollingfileappender. log4j.appender.r = org.apache.log4j.dailyrollingfileappender log4j.appender.r.file = application.log log4j.appender.r.datepattern = yyyy-mm-dd'.log' log4j.appender.r.layout = org.apache.log4j.patternlayout log4j.appender.r.layout.conversionpattern = %-d{yyyy-mm-dd hh:mm:ss} [%c]-[%p] %m%n 再创建一个servicefactory.java静态类,用来做单元测试方便一些
import org.springframework.beans.factory.beanfactory; import org.springframework.beans.factory.access.beanfactorylocator; import org.springframework.beans.factory.access.beanfactoryreference; import org.springframework.beans.factory.access.singletonbeanfactorylocator; public final class servicefactory { private static beanfactorylocator bflocator = null; private static beanfactoryreference bfreference = null; private static beanfactory factory = null; static { bflocator = singletonbeanfactorylocator.getinstance(); bfreference = bflocator.usebeanfactory("beanfactory"); factory = bfreference.getfactory(); } private servicefactory() { super(); } public static object getbeanbyname(final string beanname) { return factory.getbean(beanname); } } 做成静态的这样我们可以servicefactory .getbeanbyname(“beanname”)就可以得到相应的bean了。测试类springidetest.java代码如下:
import junit.framework.testcase; public class springidetest extends testcase { private ihelloworld helloworld = null; private ispringdemo springdemo = null; private final static string hello1 = "hello world helloworld1"; private final static string hello2 = "hello world helloworld2"; private final static string helloset = "spring ide setter"; private final static string hellocon = "spring ide constructor"; public void testspringbeans() { helloworld = (ihelloworld)servicefactory.getbeanbyname("helloworld1"); assertequals(hello1,helloworld.sayhelloworld()); helloworld = (ihelloworld)servicefactory.getbeanbyname("helloworld2"); assertequals(hello2,helloworld.sayhelloworld()); } public void testiocconstructor() { //constructor springdemo = (ispringdemo)servicefactory.getbeanbyname("springdemoconstructor"); assertequals(hellocon,springdemo.gethello()); assertequals(hello1,springdemo.gethelloworld().sayhelloworld()); } public void testiocsetter() { //setter springdemo = (ispringdemo)servicefactory.getbeanbyname("springdemosetter"); assertequals(helloset,springdemo.gethello()); assertequals(hello2,springdemo.gethelloworld().sayhelloworld()); } } run as junit test之,绿色打勾全部通过就收工走人了。。。j
当然也可以打开application.log文件查看输出一些什么信息。
使用总结
此插件的功能不错:)对于spring这样优雅的框架来说,再加上这个助手,真的是很美很美了,美美地喝上一杯咖啡吧。
闽公网安备 35060202000074号