林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka
本文將主講了spring在Eclipse下的配置,並用Spring運行了第一個HelloWorld.
一、下載需要的文件
這裏我們已經配置好Java的運行環境和裝好Eclipse了。
下載Spring
下載地址:http://maven.springframework.org/release/org/springframework/spring/
下載commons-logging
下載地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi
將它們下載後解壓到自己想放的位置,下載之前記得要看清楚是32位還是64位
二、配置Spring
1、新建一個工程,就叫SpringHelloworld。
2、添加Spring3.x的包,網上有很多不同的方法。這裏我只講一種。
在Window->Preferences->Java->Build Path->User Libraries->New添加一個用戶包的庫,這裏這麼做的原因是Spring包比較多,我們這樣做,配置一次後,以後每個工程要用直接添加該庫就行了
命名爲Spring3.2,點擊OK
添加成功後
添加到工程中來:
選擇新建的工程-》Properties->JavaBuild Path->Add library
在跳出的窗口中選擇User Library
然後又會跳出一個窗口,這時就可以選擇我們之前配置的用戶庫的包Spring3.2了,把溝打上。
添加成功
然後工程中就可以看到添加進來的Spring3.2了
三、添加commons-logging
選擇工程-》Properties->Java Build Path->Add library
然後選擇commons-logging所在的包就可以了
添加成功了
四、開始Spring編程
好了,上面的配置都弄好後,我們就可以開始第一個HelloWorld了
1.首先在當前包下新建一個HelloWorld.java
- package com.test;
- /**
- * Spring第一個HelloWorld
- * @author 林炳文(郵箱[email protected] 博客:http://blog.csdn.net/evankaka)
- * @time 2015.4.1
- */
- public class HelloWorld {
- private String info;
- public String getInfo() {
- return info;
- }
- public void setInfo(String info) {
- this.info = info;
- }
- }
package com.test;
/**
* Spring第一個HelloWorld
* @author 林炳文(郵箱[email protected] 博客:http://blog.csdn.net/evankaka)
* @time 2015.4.1
*/
public class HelloWorld {
private String info;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
2、編寫配置文件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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 配置需要被Spring管理的Bean(創建,創建後放在了Spring IOC容器裏面)-->
- <bean id="hello" class="com.test.HelloWorld">
- <!-- 配置該Bean需要注入的屬性(是通過屬性set方法來注入的) -->
- <property name="info" value="Happy New Year!"/>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置需要被Spring管理的Bean(創建,創建後放在了Spring IOC容器裏面)-->
<bean id="hello" class="com.test.HelloWorld">
<!-- 配置該Bean需要注入的屬性(是通過屬性set方法來注入的) -->
<property name="info" value="Happy New Year!"/>
</bean>
</beans>
3、反轉控制開始
在Main.java中添加如下:
- /**
- * Spring第一個HelloWorld
- * @author 林炳文(郵箱[email protected] 博客:http://blog.csdn.net/evankaka)
- * @time 2015.4.1
- */
- package com.test;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- private String who = null;
- public static void main(String[] args) {
- //獲取Spring的ApplicationContext配置文件,注入IOC容器中
- //(Map: key:String, bean標籤的id屬性值 ==>value:Object, bean標籤class屬性所指類的實例)
- BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
- HelloWorld hw1 = (HelloWorld)factory.getBean("hello");//map.get("hello")
- System.out.println(hw1.getInfo());
- System.out.println(hw1);
- }
- }
然後選擇工程右鍵:
接下來就是輸出結果啦:
林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka