. 简介
Spring Tool Suite 是一个Eclipse插件,利用该插件可以很方便的在Eclipse平台上开发基于Spring的应用。
2. 查看Eclipse版本
Spring Tool Suite插件的下载需要根据Eclipse版本而定,不同版本的Eclipse对应的插件版本不一样。
查看Eclipse版本,Window->About Eclipse ,弹出下面的对话框:
点击紫色圈起来的图标,弹出下面对话框:
可以看出clipse的版本为4.5.1.xxx版本。
3. 插件下载
4. 安装
Help->Install New Software弹出如下对话框,点击Add,输入名称spring-tool-suite,点击Archive找到我们刚才下载的文件(springsource-tool-suite-3.7.2.RELEASE-e4.5.1-updatesite.zip)。点击OK完成。
选择与IDE相关的即可:
点击Next,弹出如下对话框:
点击Finish即可完成,等待Eclipse的重启即可。
重启完成之后,Window->Preferences,如果有Spring选项表示安装成功。
5. 创建Spring HelloWorld项目
5.1 创建Project
File->New ->Java Project,输入项目名称:Spring-hello-world,点击Finish完成创建:
创建项目完成之后骨架如下:
5.2 添加Jar包
为项目添加jar包,在这里我们一共需要以下5个文件:
Spring官网改版后,很多项目的完整zip包下载链接已经隐掉了,Spring旨在引导大家使用Maven方式来管理所依赖的jar包。但是某些人某种情况下大家还是使用jar包方式。
下面是从网上搜集的一些方法:
|
以上Jar包全部的下载地址:点击打开链接
在根目录下创建lib文件,存放jar,添加到buildpath中:
5.3 添加主代码
添加包:com.sjf.spring.bean,创建HellloWorld类:
HelloWorld.java
package com.sjf.spring.bean; /*** hello world 类* @author sjf0115**/public class HelloWorld {private String name; public void setName(String name) {this.name = name;}public void sayHello(){System.out.println("welcome "+ name +" to spring world...");}}5.4 创建bean配置文件
在src目录下创建applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean --><bean id = "helloworld" class = "com.sjf.spring.bean.HelloWorld"><property name="name" value="sjf0115"></property></bean> </beans>同时为HelloWorld类进行bean配置,如何配置我们以后会介绍。
5.5 添加测试代码
分为三步:
-
创建Spring的IOC容器对象
-
从IOC容器中获取Bean实例
-
调用sayHello方法
package com.sjf.spring.test; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sjf.spring.bean.HelloWorld; /*** 测试类* @author sjf0115**/public class Test { private static ApplicationContext context;private static HelloWorld helloWorld;public static void main(String[] args) {context = new ClassPathXmlApplicationContext("applicationContext.xml");// 2. 从IOC容器中获取Bean实例helloWorld = (HelloWorld)context.getBean("helloworld");// 3.调用sayHello方法helloWorld.sayHello();}}6. 运行
一月 28, 2016 2:46:43 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org[email protected]739f3f: startup date [Thu Jan 28 14:46:42 CST 2016]; root of context hierarchy
一月 28, 2016 2:46:43 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
welcome sjf0115 to spring world...
INFO: Refreshing org[email protected]739f3f: startup date [Thu Jan 28 14:46:42 CST 2016]; root of context hierarchy
一月 28, 2016 2:46:43 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
welcome sjf0115 to spring world...