更新時間:2021-07-13 15:55:19 來源:動力節點 瀏覽2691次
下載開發包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-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,我們點擊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包
下載完成之后,我們把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框架就配置完成了。
例如:我們在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();*/
}
}
以上代碼程序運行結果如下圖所示 :
以上就是動力節點小編介紹的"Eclipse安裝配置Spring框架",希望對大家有幫助,想了解更多可查看Java基礎教程,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習