eclipse安裝配置Spring框架及基本使用


Spring核心內容的基本開發步驟

  • 下載開發包spring-tool-suite-3.9.7.RELEASE-e4.10.0-win32-x86_64,下載並導入spring-framework-5.1.4.RELEASE-dist對應的4個主要jar包和commons-logging-1.1.jar文件
  • 編寫代碼(基礎代碼和調用代碼)
  • 編寫配置文件

 Spring的開發包、如何安裝配置

下載spring-tool-suite-3.8.3.RELEASE-e4.6.2-win32文件,這個需要安裝在eclipse上面,所以必須要選擇和eclipse相對應的版本安裝,如何查看eclipse版本號?我們打開eclipse,help-About Eclipse,點擊第一個eclipse圖標,可以看到eclipse Platform的版本號。

 我們可以去http://spring.io/tools3/sts/legacy找到自己對應的eclipse版本的spring插件下載。我的eclipse版本為4.6.2,對應spring-tool-suite-3.8.3版本,因為官方下載速度太慢,附上百度網盤下載鏈接:https://pan.baidu.com/s/1tq1WscTMyb5hVE3rApwQtw 
提取碼:m34t 
下載完成之后,打開eclipse,我們點擊Help-install New Software,點擊add,進入Add Repository界面,然后點擊Archive...,選擇剛才下載的spring-tool-suite-3.9.7.RELEASE-e4.10.0-win32-x86_64.zip文件路徑點擊確定(Name為空即可),然后勾選4個帶Spring IDE的文件,點擊下一步安裝即可。

 等待安裝結束后,重啟eclipse,Spring IDE就安裝好了,會在軟件歡迎界面顯示spring IDE。

使用Spring框架,我們需要下載spring-framework-5.1.4.RELEASE-dist文件(只需要引入4個核心jar包)和commons-logging-1.1.jar包

Spring Framework各個版本下載:https://repo.spring.io/release/org/springframework/spring/

Spring任意版本都可以下載,不同版本之間的使用有部分差異,具體看官方api文檔。

下載common-logging-1.1.jar包:

鏈接:https://pan.baidu.com/s/1PC0WM7uvs3r1_cNE8j_STg 
提取碼:3gi6 

下載完成之后,我們把Spring Framework的壓縮文件解壓,找到libs文件夾,在里面找到4個jar包如下圖所示:

還有從百度網盤下載的 common-logging-1.1.jar包,總共5個jar包,將這5個jar包復制好做准備待會用。

我們打開eclipse,新建一個java project文件,隨便命名如spring-1.0,然后新建一個lib文件夾,將這5個復制好的jar包直接粘貼到lib文件夾中,如下圖所示:

 我們選定這5個jar包,然后右鍵,點擊Build Path--Add to Build Path添加到Libraries即可。

此時,Spring框架就配置完成了。

Spring框架如何使用

例如:我們在src下新建一個包,隨便命名,我把它命名為com.yorkmass.spring.beans,然后在包下面新建一個類,隨便一個類,類名如HelloWorld,里面的代碼如下:

package com.yorkmass.spring.beans;

public class HelloWorld {
	
	private String name;
	
	public void setName(String name){
		System.out.println("setName:"+name);
		this.name=name;
	}
	public void hello(){
		System.out.println("hello:"+name);
	}
	public HelloWorld(){
		System.out.println("HelloWorld's Constructor...");
	}

}

在src下面我們還需要新建一個Spring配置文件(Spring Bean Configuration File),右鍵--New--other--找到Spring文件夾下面的Spring Bean Configuration File點擊Next確定即可。

隨便命名我們這里吧配置文件命名為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.yorkmass.spring.beans.HelloWorld">
<property name="name" value="nihao"></property>
</bean>

</beans>

我們再新建一個主類,隨便命名這里我們命名為Main,Main里面的代碼如下(可以自行去掉部分注釋觀察程序運行情況):

package com.yorkmass.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		//創建HelloWorld的一個對象
		HelloWorld helloworld=new HelloWorld();
		為name屬性賦值
		helloworld.setName("test");
		*/
		//1.創建Spring的IOC容器對象
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
/*		//2.從IOC容器中獲取Bean實例
		HelloWorld helloworld=(HelloWorld)ctx.getBean("helloworld");
		//3.調用hello方法
		helloworld.hello();*/
	}

}

以上代碼程序運行結果如下圖所示 :

 


免責聲明!

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



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