Spring詳細配置 + 第一個SpringHelloWorld

一:如何在官網下載Spring需要的依賴包

 1:打開Spring官網,https://spring.io/,然後點擊projects



2:找到Spring framework ,點擊進去



3:找到github點擊,改版之後是由github託管的



4:往下拉,找到Spring Framework Artifacts模塊,點擊進去



5:往下拉,找到http://repo.spring.io模塊,點擊進去


6:在該頁面的左側,找到標籤artifacts,點擊如圖


7:繼續在該頁面的左側,找到libs-release-local,選擇org,選擇springframeworker,在選擇spring,就能看到相關版本



8:找到需要的版本,展開選擇dist.zip文件,選擇下載按鈕就ok啦



也可以直接通過該鏈接下載  http://repo.spring.io/release/org/springframework/spring/


二:依賴包包準備好了之後,配置第一個Spring  HelloWorld

 1:在Window->Preferences->Java->Build Path->User Libraries->New添加一個用戶包的庫,這裏這麼做的原因是Spring包比較多,我們這樣做,配置一次後,以後每個工程要用直接添加該庫就行了


命名爲Spring3.2,點擊OK



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



2:添加到工程中來:選擇新建的工程-》Properties->Java Build Path->Add library


在跳出的窗口中選擇User Library


然後又會跳出一個窗口,這時就可以選擇我們之前配置的用戶庫的包Spring3.2了,把溝打上


添加成功



三:開始Spring  HelloWorld編程

 1:在建立一個HelloWorld.java

package com.spring;

public class HelloWorld {  
    private String info;  
  
    public String getInfo() {  
        return info;  
    }  
  
    public void setInfo(String info) {  
        this.info = info;  
    }
}

2:在src根目錄下直接創建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.spring.HelloWorld">  
        <!-- 配置該Bean需要注入的屬性(是通過屬性set方法來注入的) -->  
        <property name="info" value="Happy New Year!"/>  
    </bean>  
</beans> 

3:在入口java類中測試函數

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);  
		 
     }

然後運行,發現會報如下錯誤:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
	at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:164)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:90)
	at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
	at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.spring.SpringTest.main(SpringTest.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	... 7 more

該錯誤是由於沒有添加commons-logging依賴包


四:添加 commons-logging依賴包

選擇工程-》Properties->Java Build Path->Add library


然後選擇commons-logging所在的包就可以了

commons-logging包的下載地址爲:http://commons.apache.org/proper/commons-logging/download_logging.cgi

重新運行就ok啦,控制檯顯示結果爲




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