Spring,spring官網
在使用Spring之前,我們要下載Spring的jar包。
,官網
http://repo.spring.io/release/org/springframework/spring/
我這裡用的3.2.9版本的Spring
解壓出jar包,其中包含編譯檔案、文檔、源碼
我們說下每個jar都是幹什麼的。
org.springframework.aop ——Spring的面向切面編程,提供AOP(面向切面編程)的實現
org.springframework.asm——spring 2.5.6的時候需要asm jar包,spring3.0開始提供它自己獨立的asm jar包
org.springframework.aspects——Spring提供的對AspectJ架構的整合
org.springframework.beans——所有應用都用到,包含訪問設定檔,建立和管理bean等,是Spring IOC的基礎實現。
org.springframework.context.support——Spring context的擴充支援,用於MVC方面
org.springframework.context——提供在基礎IOC功能上的擴充服務,此外還提供許多企業級服務的支援,有郵件服務、任務調度、JNDI定位,EJB整合、遠端存取、緩衝以及多種視圖層架構的支援。
org.springframework.core——Spring的核心工具包,其他包依賴此包
org.springframework.expression——Spring運算式語言
org.springframework.instrument.tomcat——Spring對tomcat串連池的整合
org.springframework.instrument——Spring對伺服器的代理介面
org.springframework.jdbc——對JDBC 的簡單封裝
org.springframework.jms——為簡化jms api的使用而做的簡單封裝
org.springframework.orm——整合第三方的orm實現,如hibernate,ibatis,jdo以及spring 的jpa實現
org.springframework.oxm——Spring對於object/xml映射的支援,可以讓JAVA與XML之間來回切換
org.springframework.test——對JUNIT等測試架構的簡單封裝
org.springframework.transaction——為JDBC,HIBERNATE,JDO和JPA提供的一致性的聲明式和簡單編程式交易管理
org.springframework.web.portlet——Spring MVC的增強
org.springframework.web.servlet——對J2EE6.0 servlet3.0的支援
org.springframework.web.struts——整合struts架構的支援,可以更方便更容易的整合Struts架構。
org.springframework.web——包含Web應用開發時,用到Spring架構時所需的核心類,包括自動載入WebApplicationContext特性的類、Struts與JSF整合類、檔案上傳的支援類、Filter類和大量工具輔助類。
下面我們來第一個簡單程式,要實現的內容如
- 引入所有jar,其中要引入apache-common-log這個jar包。
- 建立一個介面類
package com.gp.spring01;public interface IHelloWorld { public void sayHello();}
package com.gp.spring01;public class HelloWorld implements IHelloWorld { @Override public void sayHello() { System.out.println("Say Hello"); }}
package com.gp.spring01;public class HelloChina implements IHelloWorld { @Override public void sayHello() { System.out.println("Hello World"); }}
package com.gp.spring01;public class Person { IHelloWorld iHelloWorld; public IHelloWorld getiHelloWorld() { return iHelloWorld; } public void setiHelloWorld(IHelloWorld iHelloWorld) { this.iHelloWorld = iHelloWorld; } public void say() { iHelloWorld.sayHello(); }}
分析此類直接調用介面中的方法,並沒有指定介面的實作類別。
package com.gp.spring01;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;public class Main { public static void main(String[] args) { Resource r = new FileSystemResource("helloMessage.xml"); BeanFactory f = new XmlBeanFactory(r); Person person = (Person) f.getBean("person"); person.say(); }}
注意代碼中載入了helloMessage.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="helloWord" class="com.gp.spring01.HelloWorld" /> <bean id="helloChina" class="com.gp.spring01.HelloChina" /> <bean id="person" class="com.gp.spring01.Person"> <property name="iHelloWorld" ref="helloChina"></property> </bean></beans>
此xml起到的是IOC容器的作用,它將各Bean建立聯絡,代碼中8-10行,將Person對象的iHelloWorld介面指向helloChina完成相互之間的依賴。如果我們要實現helloChina介面,只需改成如下即可
<bean id="person" class="com.gp.spring01.Person"> <property name="iHelloWorld" ref="helloChina"></property> </bean>
複製上面的代碼,可以在你的本地運行下啊~~~~
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。