spring 框架使用初步

12/30/2009来源:Delphi教程人气:10496

由于刚刚开始学习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(lib\jakarta-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.support.ClassPathXmlApplicationContext@ecd7e]; startup date [Wed Dec 30 09:03:28 CST 2009]; root of context hierarchy   
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.support.ClassPathXmlApplicationContext@ecd7e]; startup date [Wed Dec 30 09:03:28 CST 2009]; root of context hierarchy
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)   
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)   
    at my.sh.spring.springtest.main(springtest.java:9)  



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/msjqd/archive/2009/12/30/5103718.aspx