SSM搭建-Spring第一個Spring HelloWorld(2)

          林炳文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


點擊Add External JARS.在跳出的窗口中選擇Spring libs的包所在的位置(看你的解壓位置),把用到的JAR都添加進來


添加成功後

添加到工程中來:

選擇新建的工程-》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

[java] 
  1. package com.test;  
  2. /** 
  3.  * Spring第一個HelloWorld 
  4.  * @author 林炳文(郵箱[email protected] 博客:http://blog.csdn.net/evankaka) 
  5.  * @time 2015.4.1 
  6.  */  
  7. public class HelloWorld {  
  8.     private String info;  
  9.   
  10.     public String getInfo() {  
  11.         return info;  
  12.     }  
  13.   
  14.     public void setInfo(String info) {  
  15.         this.info = info;  
  16.     }  
  17.       
  18.   
  19. }  

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內容改爲如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    
  7.     <!-- 配置需要被Spring管理的Bean(創建,創建後放在了Spring IOC容器裏面)-->  
  8.     <bean id="hello" class="com.test.HelloWorld">  
  9.         <!-- 配置該Bean需要注入的屬性(是通過屬性set方法來注入的) -->  
  10.         <property name="info" value="Happy New Year!"/>  
  11.     </bean>  
  12. </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中添加如下:

  1. /** 
  2.  * Spring第一個HelloWorld 
  3.  * @author 林炳文(郵箱[email protected] 博客:http://blog.csdn.net/evankaka) 
  4.  * @time 2015.4.1 
  5.  */  
  6. package com.test;  
  7. import org.springframework.beans.factory.BeanFactory;  
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  9. public class Main {  
  10.   
  11.     private String who = null;  
  12.   
  13.     public static void main(String[] args) {  
  14.         //獲取Spring的ApplicationContext配置文件,注入IOC容器中  
  15.         //(Map: key:String, bean標籤的id屬性值 ==>value:Object, bean標籤class屬性所指類的實例)  
  16.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
  17.         HelloWorld hw1 = (HelloWorld)factory.getBean("hello");//map.get("hello")  
  18.         System.out.println(hw1.getInfo());  
  19.         System.out.println(hw1);  
  20.   
  21.     }  
  22. }  



然後選擇工程右鍵:

接下來就是輸出結果啦:


  本文工程下載

林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章