【Spring】快速入門

1.Spring介紹
Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是爲了解決企業應用開發的複雜性而創建的。Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限於服務器端的開發。從簡單性、可測試性和松耦合的角度而言,任何Java應用都可以從Spring中受益
簡單來說,Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器框架。

2.Spring體系結構
Spring 框架是一個分層架構,,它包含一系列的功能要素並被分爲大約20個模塊。這些模塊分爲Core Container、Data Access/Integration、Web、AOP(Aspect Oriented Programming)、Instrumentation和測試部分,如下圖所示:

這裏寫圖片描述

3.使用Spring優勢

  • 方便解耦,簡化開發
    Spring就是一個大工廠,可以將所有對象創建和依賴關係維護,交給Spring管理
  • AOP編程的支持
    Spring提供面向切面編程,可以方便的實現對程序進行權限攔截、運行監控等功能
  • 聲明式事務的支持
    只需要通過配置就可以完成對事務的管理,而無需手動編程
  • 方便程序的測試
    Spring對Junit4支持,可以通過註解方便的測試Spring程序
  • 方便集成各種優秀框架
    Spring不排斥各種優秀的開源框架,其內部提供了對各種優秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
  • 降低JavaEE API的使用難度
    Spring 對JavaEE開發中非常難用的一些API(JDBC、JavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大降低

4.Spring 入門程序

  • 4.1 編寫流程
    下載Spring最新開發包
    複製Spring開發 jar包到工程
    編寫Spring核心配置文件
    在程序中讀取Spring配置文件,通過Spring框架獲得Bean,完成相應操作

  • 4.2 HelloWorld實現

    4.2.1 下載jar
    核心包:spring-framework-3.2.2.RELEASE-dist.zip
    第三方依賴包:spring-framework-3.0.2.RELEASE-dependencies.zip
    包結構:com.springsource

    4.2.2 Spring核心開發包
     spring-core-3.2.2.RELEASE.jar
    包含Spring框架基本的核心工具類,Spring其它組件要都要使用到這個包裏的類,是其它組件的基本核心。
     spring-beans-3.2.2.RELEASE.jar
    所有應用都要用到的,它包含訪問配置文件、創建和管理bean, 以及進行Inversion of Control(IoC) / Dependency Injection(DI)操作相關的所有類
     spring-context-3.2.2.RELEASE.jar
    Spring提供在基礎IoC功能上的擴展服務,此外還提供許多企業級服務的支持,如郵件服務、任務調度、JNDI定位、EJB集成、遠程訪問、緩存以及各種視圖層框架的封裝等。
     spring-expression-3.2.2.RELEASE.jar
    Spring表達式語言
     com.springsource.org.apache.commons.logging-1.1.1.jar
    第三方的主要用於處理日誌

    4.2.3 編寫業務類

public class HelloService {

    private String info;    //普通字段
    public void setInfo(String info){   //爲字段提供setter方法
        this.info = info;
    }

    public void sayInfo(){
        System.out.println("你好," + info);
    }
}

傳統方式創建實例
public class TestHello {

    public static void main(String[] args) {
        //傳統自己創建new
        HelloService service = new HelloService();
    }
}

Spring IoC控制反轉創建實例,編寫配置文件( 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloService" class="cn.spring.demo.hello.HelloService">
    </bean>
</beans>

從spring容器獲得對象實例
public class TestHello {
    public static void main(String[] args) {
        //使用spring 控制反轉,交予spring創建對象
        ApplicationContext applicationContext = 
new ClassPathXmlApplicationContext("classpath:config/beans.xml");
        HelloService servcie = 
(HelloService) applicationContext.getBean("helloService");
    }
}

IoC解釋
IoC Inverse of Control 反轉控制的概念,就是將原本在程序中手動創建HelloService對象的控制權,交由Spring框架管理,簡單說,就是創建HelloService對象控制權被反轉到了Spring框架

傳統方式設置內容
public class TestHello {    
    public static void main(String[] args) {
        //傳統自己創建new
        HelloService service = new HelloService();
        service.setInfo("lt");  //手動設置內容
        service.sayInfo();
    }
}

Spring DI設置字段內容,編寫配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="helloService" class="cn.itcast.a.hello.HelloService">
        <!—通過spring容器,給HelloService類的info屬性注入“你好spring”-->
        <property name="info" value="你好spring"></property>
    </bean>
</beans>

從spring直接獲得注入的內容
public class TestHello {    
    public static void main(String[] args) {
        //使用spring 控制反轉,交予spring創建對象
        ApplicationContext applicationContext = 
new ClassPathXmlApplicationContext("classpath:config/beans.xml");
        HelloService servcie = 
(HelloService) applicationContext.getBean("helloService");

        //servcie.setInfo("你好spring");
        servcie.sayInfo();
    }

}

DI解釋
DI:Dependency Injection 依賴注入在Spring框架負責創建Bean對象時,動態的將依賴對象注入到Bean組件。getBean(“helloService”)從spring容器中獲得指定名稱對象的實例時,通過此設置 相當於執行 servcie.setInfo(“你好spring”);

  • 4.3 加載spring 容器的三種方式

    4.3.1 類路徑獲得配置文件
    ApplicationContext applicationContext =new ClassPathXmlApplicationContext(“classpath:config/beans.xml”);
    ClassPathXmlApplicationContext(String)加載一個spring容器
    ClassPathXmlApplicationContext(String[])加載多個spring容器

    4.3.2 文件系統路徑獲得配置文件
    ApplicationContext applicationContext =
    new FileSystemXmlApplicationContext(“F:\workspaces\spring\day01\src\cn\spring\demo\hello\beans.xml”);

    4.3.3 使用BeanFactory(瞭解)
    BeanFactory beanFactory = new
    XmlBeanFactory(new FileSystemResource(“F:\workspaces\spring\day01\src\cn\spring\demo\hello\beans.xml”));

    4.3.4 BeanFactory和ApplicationContext對比
    BeanFactory 採取延遲加載,第一次getBean時纔會初始化Bean
    ApplicationContext是對BeanFactory擴展,提供了更多功能(國際化處理、事件傳遞、Bean自動裝配、 各種不同應用層的Context實現)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章