1,准備工作
(1)安裝spring插件
搜索https://spring.io/tools/sts/all就可以下載最新的版本
下載之后不用解壓,使用Eclipse進行安裝。在菜單欄最右面的Help菜單:
點擊Install New Software之后,有如下界面,按次序點擊找到剛才下載的安裝包之后確認。
確認之后到下面這個界面之后,注意只需要選擇其中的某些選項就可以了。
下面一路安裝,之后重啟就可以,可以看看是否安裝成功,如下表示成功。
(2)下載jar包。
spring-framework-4.2.4.RELEASE-dist 下載地址:http://repo.spring.io/release/org/springframework/spring/
commons-logging-1.1.1 網上自行下載。
2,開始HelloWorld的小程序
(1)新建項目,添加一個lib文件,把剛才下載的包中的下面這些包拷貝進去,全選jar包,右鍵Build Path ---〉Add to Build Path
(2)新建下面3個文件:HelloWorld.java; Main.java ;applicationContext.xml
HelloWorld.java 代碼:
package com.yfy; public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void hello(){ System.out.println("hello: " + name); } }
Main.java 代碼:
package com.yfy; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld) applicationContext.getBean("helloWorld"); helloWorld.hello(); }}
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.yfy.HelloWorld"> <property name="name" value="spring"></property> </bean> </beans>
運行Main.java
下面就詳細分析一下:
注:以上學習筆記參考: 尚硅谷_佟剛_Spring_HelloWorld第一課,佟剛老師講的不錯,慢慢學習。