文章出處

Spring是什么?

Spring 是一個開源框架.

Spring 為簡化企業級應用開發而生.

使用 Spring 可以使簡單的 JavaBean 實現以前只有 EJB 才能實現的功能.

Spring 是一個 IOC(DI) 和 AOP 容器框架.

具體描述 Spring:

--輕量級:Spring 是非侵入性的 - 基于 Spring 開發的應用中的對象可以不依賴于 Spring 的API

--依賴注入(DI --- dependency injection、IOC)

--面向切面編程(AOP --- aspect oriented programming)

--容器: Spring 是一個容器, 因為它包含并且管理應用對象的生命周期

--框架: Spring 實現了使用簡單的組件配置組合成一個復雜的應用. 在 Spring 中可以使用 XML 和 Java 注解組合這些對象

--一站式:在 IOC 和 AOP 的基礎上可以整合各種企業應用的開源框架和優秀的第三方類庫 (實際上 Spring 自身也提供了展現層的 SpringMVC 和 持久層的 Spring JDBC)

只要后面是Spring IDE就行了

把以下 jar 包加入到工程的 classpath 下:

Spring 的配置文件: 一個典型的 Spring 項目需要創建一個或多個 Bean 配置文件, 這些配置文件用于在 Spring IOC 容器里配置 Bean. Bean 的配置文件可以放在 classpath 下, 也可以放在其它目錄下

一、IOC 和 DI

IOC(Inversion of Control):其思想是反轉資源獲取的方向. 傳統的資源查找方式要求組件向容器發起請求查找資源. 作為回應, 容器適時的返回資源. 而應用了 IOC 之后, 則是容器主動地將資源推送給它所管理的組件, 組件所要做的僅是選擇一種合適的方式來接受資源. 這種行為也被稱為查找的被動形式

DI(Dependency Injection) — IOC 的另一種表述方式:即組件以一些預先定義好的方式(例如: setter 方法)接受來自如容器的資源注入. 相對于 IOC 而言,這種表述更直接

配置 bean

--配置形式:基于 XML 文件的方式;基于注解的方式

--Bean 的配置方式:通過全類名(反射)、通過工廠方法(靜態工廠方法 & 實例工廠方法)、FactoryBean

--IOC 容器 BeanFactory & ApplicationContext 概述

--依賴注入的方式:屬性注入;構造器注入

--注入屬性值細節

--自動轉配

--bean 之間的關系:繼承;依賴

--bean 的作用域:singleton;prototype;WEB 環境作用域

--使用外部屬性文件

--spEL

--IOC 容器中 Bean 的生命周期

--Spring 4.x 新特性:泛型依賴注入

1-1.基于 XML 文件的方式配置 bean

public class HelloWorldBean {    private String user;    public HelloWorldBean() {        System.out.println("HelloWorldBean's constructor...");    }    // 如果這里修改為setUser2,剛applicationContext.xml修改為property name="user2"    public void setUser(String user) {        System.out.println("setUser:" + user);        this.user = user;    }    public HelloWorldBean(String user) {        this.user = user;    }    public void hello(){        System.out.println("Hello: " + user);    }}

applicationContext.xml的關鍵內容如下:

                    

id:Bean 的名稱。

在 IOC 容器中必須是唯一的

若 id 沒有指定,Spring 自動將權限定性類名作為 Bean 的名字

id 可以指定多個名字,名字之間可用逗號、分號、或空格分隔

    @Test    public void testHelloWorld() {        //        前兩行可以用Spring來實現//        HelloWorldBean helloWorldBean = new HelloWorldBean();//        helloWorldBean.setUser("Tom");//        helloWorldBean.hello();        //1. 創建 Spring 的 IOC 容器(ApplicationContext代表Spring 的 IOC 容器)        //創建的時候會先調用無參構造器,同時會調用 setter方法對屬性賦值!!        ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/applicationContext.xml");        System.out.println("------------------------------");        //2. 從 IOC 容器中的id獲取 bean 的實例,如果為“helloWorld2”,則bean id="helloWorld2"        HelloWorldBean helloWorldBean = (HelloWorldBean) ctx.getBean("helloWorld");        //根據類型來獲取 bean 的實例: 要求在  IOC 容器中只有一個與之類型匹配的 bean, 若有多個則會拋出異常.        //一般情況下, 該方法可用, 因為一般情況下, 在一個 IOC 容器中一個類型對應的 bean 也只有一個.//       HelloWorldBean helloWorld1 = ctx.getBean(HelloWorldBean.class);        //3. 使用 bean        helloWorldBean.hello();    }

HelloWorldBean's constructor...

setUser:Tom

------------------------------

Hello: Tom

1-1-1.IOC 容器

在 Spring IOC 容器讀取 Bean 配置創建 Bean 實例之前, 必須對它進行實例化. 只有在容器實例化后, 才可以從 IOC 容器里獲取 Bean 實例并使用.

Spring 提供了兩種類型的 IOC 容器實現.

--BeanFactory: IOC 容器的基本實現.

--ApplicationContext: 提供了更多的高級特性. 是 BeanFactory 的子接口.

--BeanFactory 是 Spring 框架的基礎設施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的開發者,幾乎所有的應用場合都直接使用 ApplicationContext 而非底層的 BeanFactory

--無論使用何種方式, 配置文件時相同的.

ApplicationContext

ApplicationContext 的主要實現類:

--ClassPathXmlApplicationContext:從 類路徑下加載配置文件

--FileSystemXmlApplicationContext: 從文件系統中加載配置文件

ConfigurableApplicationContext 擴展于 ApplicationContext,新增加兩個主要方法:refresh() 和 close(), 讓 ApplicationContext 具有啟動、刷新和關閉上下文的能力

ApplicationContext 在初始化上下文時就實例化所有單例的 Bean。

WebApplicationContext 是專門為 WEB 應用而準備的,它允許從相對于 WEB 根目錄的路徑中完成初始化工作

1-1-2.依賴注入的方式

Spring 支持 3 種依賴注入的方式

--屬性注入

--構造器注入

--工廠方法注入(很少使用,不推薦)

1).屬性注入

屬性注入即通過 setter 方法注入Bean 的屬性值或依賴的對象

屬性注入使用 元素, 使用 name 屬性指定 Bean 的屬性名稱,value 屬性或 子節點指定屬性值

屬性注入是實際應用中最常用的注入方式

在applicationContext.xml

                                                                
2).構造方法注入

通過構造方法注入Bean 的屬性值或依賴的對象,它保證了 Bean 實例在實例化后就可以使用。

構造器注入在 元素里聲明屬性, 中沒有 name 屬性

public class Car {    private String company;    private String brand;    private int maxSpeed;    private float price;    public Car() {    }    public Car(String brand, float price) {        super();        this.brand = brand;        this.price = price;    }    public Car(String company, String brand, float price) {        super();        this.company = company;        this.brand = brand;        this.price = price;    }    public CarCycle(String company, String brand, int maxSpeed) {        super();        this.company = company;        this.brand = brand;        this.maxSpeed = maxSpeed;    }    public Car(String company, String brand, int maxSpeed, float price) {        super();        this.company = company;        this.brand = brand;        this.maxSpeed = maxSpeed;        this.price = price;    } //省去get,set方法    @Override    public String toString() {        return "CarCycle [company=" + company + ", brand=" + brand + ", maxSpeed="                + maxSpeed + ", price=" + price + "]";    }}
                                                                                                               

字面值:可用字符串表示的值,可以通過 元素標簽或 value 屬性進行注入。

基本數據類型及其封裝類、String 等類型都可以采取字面值注入的方式

若字面值中包含特殊字符,可以使用 把字面值包裹起來。

    /***********     * Bean 的配置方式:-、通過全類名(反射);二、通過工廠方法(靜態工廠方法 & 實例工廠方法);三、FactoryBean     *     * 配置Bean:配置方式:通過全類名(反射)***************/    //測試依賴注入    @Test    public void testDependencyInjection() {        ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/applicationContext.xml");        Car car = (Car) ctx.getBean("car");        System.out.println("1-->"+car);        //測試car里有多個構造器,如何配置IOC        Car car2 = (Car) ctx.getBean("car2");        System.out.println("2-->"+car2);    }

1-->CarCycle [company=KUGA, brand=ChangAnFord, maxSpeed=0, price=250000.0]

2-->CarCycle [company=ChangAnMazda, brand=ShangHai, maxSpeed=180, price=0.0]

3).注入屬性值細節

引用其它 Bean

組成應用程序的 Bean 經常需要相互協作以完成應用程序的功能. 要使 Bean 能夠相互訪問, 就必須在 Bean 配置文件中指定對 Bean 的引用

在 Bean 的配置文件中, 可以通過 元素或 ref 屬性為 Bean 的屬性或構造器參數指定對 Bean 的引用.

也可以在屬性或構造器里包含 Bean 的聲明, 這樣的 Bean 稱為內部 Bean

                                                        

內部Bean

--當 Bean 實例僅僅給一個特定的屬性使用時, 可以將其聲明為內部 Bean. 內部 Bean 聲明直接包含在 元素里, 不需要設置任何 id 或 name 屬性

--內部 Bean 不能使用在任何其他地方

注入參數詳解:null 值和級聯屬性

--可以使用專用的 元素標簽為 Bean 的字符串或其它對象類型的屬性注入 null 值,意義不大

--和 Struts、Hiberante 等框架一樣,Spring 支持級聯屬性的配置,例如在xml文件里設置Car.price

就愛閱讀www.92to.com網友整理上傳,為您提供最全的知識大全,期待您的分享,轉載請注明出處。
歡迎轉載:http://www.kanwencang.com/bangong/20161102/33006.html

文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()