由於剛開始學習spring,對於他的理解也不是非常的透徹,從我個人的角度來說,能夠使用他進行mvc開發就已經足夠了。
目標:能夠把spring框架整合到開發環境中。
工具:eclipse + spring2.5.6
步驟:1、下載eclipse 或是myeclipse 依照個人喜好從網路下載。
2、下載spring2.5.6(目前最高版本是3.0) 到spring官方網站下載http://www.springsource.org/download 。注意:下載前
需要填寫個人資料。要下載spring-framework-2.5.6-with-dependencies版本
3、解壓spring。 spring要運作必須需要兩個jar文件
spring.jar(dist目錄下面)
commons-logging.jar(libjakarta-commons目錄下面)
4.接下來就是使用eclipse建立一個工程(java 或web工程都可以)
5.把上面的兩個jar檔加入到工程中。
6.測試是否整合成功
view plaincopy to clipboardprint?
package my.sh.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class springtest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
package my.sh.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class springtest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
觀察控制台輸出
正確輸出:
view plaincopy to clipboardprint?
2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e : display name [org.springframework.context.ClassPathXmlApplicationContext@ecd7e : display name [org.springframework.context.ClassPathXmlApplicationContext@ecd7e : display name [org.springframework.context.ClassPathXmlApplicationContext@ecd7e]; startup date [Wed Decport.ClassPathXmlApplicationContext@ecd7e];
2009-12-30 9:03:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [applicationContext.xml]
2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
資訊: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128
2009-12-30 9:03:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128 : defining beans []; root of factory hierarchy
2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e : display name [org.springframework.context.ClassPathXmlApplicationContext@ecd7e : display name [org.springframework.context.ClassPathXmlApplicationContext@ecd7e : display name [org.springframework.context.ClassPathXmlApplicationContext@ecd7e]; startup date [Wed Decport.ClassPathXmlApplicationContext@ecd7e];
2009-12-30 9:03:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [applicationContext.xml]
2009-12-30 9:03:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
資訊: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128
2009-12-30 9:03:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@60e128 : defining beans []; root of factory hierarchy
如果你沒有引用commons-logging.jar ,會輸出如下錯誤提示
view plaincopy to clipboardprint?
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:145)
at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:84)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:58)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
在 org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
在 my.sh.spring.springtest.main(springtest.java:9)
本文來自CSDN博客,轉載請標示出處: http://blog.csdn.net/msjqd/archive/2009/12/30/5103718.aspx
-