2014年5月24日

Spring 手札 第一章 基本環境設定

 spring環境設定

1. Eclipse 安裝gradle套件和spring套件



2.新增一個專案為spring資料夾並新增build.gradle檔案

build.gradle內容 : 抓取spring的套件

/* 引用 java plugin 獲得編譯 java 專案相關的 task $ */
apply plugin: 'java'

/* 引用 eclipse plugin 獲得編譯 java 專案相關的 task $ */
apply plugin: 'eclipse'

/* 設定 maven repository server $ */
repositories { mavenCentral() }
dependencies {
    compile 'org.springframework:spring-context:4.0.0.RELEASE'
}




3.Run As : Gradle GUI 開始後,使用選則command line: 打上eclipse並執行




輸入 : eclipse




4. Hellow World 範例
    (1) 新增 Spring Bean Configuration File 命名為 applicationContext.xml






(2)建立HelloWorld.java
package test;

public class HelloWorld {
public void show(){
System.out.println("spfing hellow world!");
}
}

(3) 編輯 applicationContext.xml基本屬性 id和class
<?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="test" class="test.HelloWorld"></bean>


</beans>

(4)測試 : TestHelloWorld.java

package test;

public class HelloWorld {
public void show(){
System.out.println("spfing hellow world!");
}
}

成功後,主控台會出現結果如下: