Eclipse安裝springsource-tool-suite插件及spring helloworld入門實例


一、查看eclipse版本

Help-->About Eclipse,我的版本是4.4.2。

二、根據eclipse 版本,選擇插件版本

訪問網址:http://spring.io/tools/sts/all 查看eclipse對應的插件版本

eclipsep安裝spring插件有兩種方式:在線安裝和本地安裝;

1、在線安裝

Help-->Install New Software-->work with 中輸入http://dist.springsource.com/release/TOOLS/update/e4.4/ ,回車等待片刻,選中帶spring IDE的四項,一直next直到完成,重啟eclipse。

2、本地安裝

Help-->Install New Software-->Add-->Local-->選擇解壓文件夾,剩下的和在線安裝類似。

三、下載spring jar包

 訪問http://projects.spring.io/spring-framework/,可以看到最新的穩定版本為4.2.4

點擊Reference-->點擊Distribution Zip Files-->點擊http://repo.spring.io/release/org/springframework/spring

    

選擇最新的穩定版本4.2.4.RELEASE

打開頁面,根據提示下載需要的文件

四、HelloWorld入門實例

1、新建java工程,命名HelloSpring;新建Folder 命名為lib,存放Spring基本的jar包和commons-logging包(可在Struts的lib中找到),並將這些包Add Build Path。項目結構圖所下圖所示:

2、HelloWorld.java代碼

package com.ke361;

public class HelloWorld {
    private String message;
       public void setMessage(String message){
          this.message=message;
       }
       public void getMessage(){
          System.out.println("Your Message : " + message);
       }
}

3、新建spring配置文件

在src下新建spring配置文件,命名為applicationContext.xml。

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 id="helloWorld" class="com.ke361.HelloWorld">
       <property name="message" value="Hello World!"/>
</bean>
</beans>

4、MainApp.java代碼

package com.ke361;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
          HelloWorld obj=(HelloWorld)context.getBean("helloWorld");
          obj.getMessage();
    }

}

5、運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM