一、Spring概述java
--Spring是一個開源的以控制反轉(Inverse(相反) of Control,IoC)和麪向切面(Aspect(方向) Oriented(定向) Programming,AOP)爲核心的框架。spring
用於簡化企業級開放。編程
--控制反轉:應用自己不負責依賴對象的建立及維護,依賴對象的建立及維護由外部容器負責,app
此時對依賴對象的控制權就由應用轉移到了外部容器。這種控制權的轉移就稱爲控制反轉。框架
運行時,可經過依賴注入的方式將對象注入到應用的組件中。測試
--面向切面:也稱爲面向切面編程,它是面向對象編程(OOP)的不補充和完善。this
2.Spring框架.net
Core Container核心容器,Spring的其餘模塊都是創建在覈心容器之上。xml
Beans和Core模塊實現了Spring框架的最基本功能,規定了建立、配置和管理Bean的方式,提供了控制反轉(IoC)和依賴注入(DI)的特性。對象
Context模塊創建在Core和Beans模塊之上,該模塊向Spring框架提供了上下文信息。
Expression Language模塊提供了一種強大的表達式語言來訪問和操縱運行時的對象。
Data Access/Integration爲數據訪問/集成模塊
三、實驗步驟
①下載架包:http://download.csdn.net/detail/yy228313/7320815
②建立一個java項目
③搭建Spring運行環境
④建立一個Peron接口及其的兩個實現類American和Chinese。
⑤建立一個PersonService業務類,其中包括一個方法:sayHello( String name),在該方法中調用Person接口的hello方法
⑥在src目錄下新建一個Spring的配置文件
4.代碼
Person接口和兩個實現類:
package com.sise.ye.service.inter; /** * 項目名稱:javaII-11 * 包名:com.sise.ye.service.inter * 類名:Person.java * 建立人:葉曉東 * 建立時間:2014-5-8 * 描述:接口 * 備註: * @version 1.0 */ public interface Person { public String hello(String username); } package com.sise.ye.service.impl; import com.sise.ye.service.inter.Person; /** * 項目名稱:javaII-11 * 包名:com.sise.ye.service.impl * 類名:American.java * 建立人:葉曉東 * 建立時間:2014-5-8 * 描述:實現類(實現Person) * 備註: * @version 1.0 */ public class American implements Person{ public String hello(String username){ return "Hello,"+username+"! I come from American."; } } package com.sise.ye.service.impl; import com.sise.ye.service.inter.Person; /** * 項目名稱:javaII-11 * 包名:com.sise.ye.service.impl * 類名:Chinese.java * 建立人:葉曉東 * 建立時間:2014-5-8 * 描述:實現類(實現Person) * 備註: * @version 1.0 */ public class Chinese implements Person{ public String hello(String username){ return "您好,"+username+"!我來自中國。"; } }
業務類:
package com.sise.service; import com.sise.ye.service.inter.Person; /** * 項目名稱:javaII-11 * 包名:com.sise.service * 類名:PersonServiceBean.java * 建立人:葉曉東 * 建立時間:2014-5-8 * 描述:業務類 * 備註: * @version 1.0 */ public class PersonServiceBean { private Person person; public void sayHello(String username){ System.out.println(person.hello(username)); } //注入 public void setPerson(Person person){ this.person=person; } }
applicationContext.xml(Spring的配置文件)
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="chinese" class="com.sise.ye.service.impl.Chinese"/> <bean id="american" class="com.sise.ye.service.impl.American"/> <bean id="personservice" class="com.sise.service.PersonServiceBean"> <property name="person" ref="american"/> </bean> </beans>
測試類:
package com.sise.ye.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sise.service.PersonServiceBean; /** * 項目名稱:javaII-11 * 包名:com.sise.ye.test * 類名:SpringTest.java * 建立人:葉曉東 * 建立時間:2014-5-8 * 描述:測試類 * 備註: * @version 1.0 */ public class SpringTest { public static void main(String args[]){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonServiceBean psb=(PersonServiceBean)ctx.getBean("personservice"); psb.sayHello("曉東"); } }
5.代碼分析
6.代碼下載