---
title: "尚学堂_Sping_0200_IOC_Introduction"
author: "listen-raining"
date: "2014-10-10T11:00:06Z"
tags: []
categories: ["企业架构"]
slug: "sping-0200-ioc-introduction"
draft: false
lastmod: "2014-10-10T11:00:06Z"
id: 2127007
url: "https://listen-raining.iteye.com/blog/2127007"
---

Sping\_0200\_IOC\_Introduction(Spring2.5环境搭建)

 

首先，通过Spring Framework -Reference Documentation（E:\\source\\doc\\spring-framework-2.5.6-with-dependencies\\spring-framework-2.5.6\\docs\\reference\\html\_single\\index.html）引入Spring2.5的xml配置文件：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;

 

  &lt;bean id="..." class="..."&gt;

    &lt;!-- collaborators and configuration for this bean go here --&gt;

  &lt;/bean&gt;

 

     &lt;bean id="..." class="..."&gt;

    &lt;!-- collaborators and configuration for this bean go here --&gt;

  &lt;/bean&gt;

 

  &lt;!-- more bean definitions go here --&gt;

 

&lt;/beans&gt;

 

 

其次，向项目中添加spring对应的jar包

第一：Window--&gt;Preference--&gt;Java--&gt;Build Path--&gt;new(new一个userLib)，然后点击Add Jars导入Spring.jar文件。

 

第二：选择项目右键，Build Path--&gt;add Libraries--&gt;User Library--&gt;选择刚建的userLib，这样就将Spring.jar添加到项目中了。

 

 

 

最后，进行Junit测试

 

Failure Trace

java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory  

原因：缺少日志jar包， 添加commons-logging.jar包

 

 

 

 

测试方法：

 

public class UserServiceTest {

 

@Test

public void testAdd()throws Exception {

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");

UserService service=(UserService)applicationContext.getBean("userService");

User u=new User();

service.add(u);

}

 

 

}

 

 

 

 

 

 

                                      @longkouzhonglu.tianhequ.guangzhou        2014-10-10 10:57