Spring學習之旅(四)Spring工作原理再探,spring工作原理

來源:互聯網
上載者:User

Spring學習之旅(四)Spring工作原理再探,spring工作原理

上篇博文對Spring的工作原理做了個大概的介紹,想看的同學請出門左轉。今天詳細說幾點。

(一)Spring IoC容器及其執行個體化與使用

Spring IoC容器負責Bean的執行個體化、配置和組裝工作有兩個介面:BeanFactory和ApplicationContext。其中ApplicationContext繼承於BeanFactory,對企業級應用開發提供了更多的支援。在實際應用中都是用該介面。

1)執行個體化Spring容器(主要有四種)

1.ClassPathXmlApplicationContext: 在類路徑下尋找配置XML檔案執行個體化容器。

ApplicationContext act = new ClassPathXmlApplicationContext("hellobean.xml");

2.FileSystemXmlApplicationContext:在檔案系統路徑下尋找設定檔來執行個體化容器。

ApplicationContext act=new FileSystemXmlApplicationContext("d:/beans.xml");

3.XmlWebApplicationContext:從Web應用目錄WEB-INF中的XML設定檔執行個體化容器。(小編未能實現成功,請實現成功的同學指教)

WebApplicationContext wctx = new XmlWebApplicationContext();

4.在web.xml設定檔中,通過配置監聽器執行個體化容器。(假定已經配置了Spring的設定檔)

Spring設定檔可指定多個,之間用逗號隔開。

//在網頁中通過request對象或其他方式,擷取Web伺服器容器  ServletContext sc=request.getServletContext();//利用spring架構提供的靜態方法,從Web伺服器中擷取Spring容器  WebApplicationContext wact=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

2)產生Bean執行個體

Spring容器通過getBean()方法,從容器中擷取所管理的對象。

例如:

HelloBeans student=(HelloBeans)wctx.getBean("stu1");

(二)基於XML檔案方式的Bean配置

在java容器中形成Bean稱為裝配。

Bean的裝配形式有兩種:基於XML檔案的方式和基於註解的方式。

基於XML檔案的方式就是用一個XML檔案對Bean資訊實施配置。主要有兩部分:命名空間、Bean及有關資訊的配置。

4種配置Bean的方法:

例子:定義兩個實體類

public class Address {    private String city;    private String school;    //無參構造器    public Address(){        this.city="taian";        this.school="nongda";    }    //有參構造器    public Address(String city,String school){        this.city=city;        this.school=school;    }    //省略了setter/getter方法}
public class Student {    private String name;    private int age;    Address address;    //預設構造器    public Student(){}    //有參構造器    public Address(String name,int age,Address address){        this.name=name;        this.age=age;        this.address=address;    }    //省略了setter/getter方法}

第1種配置方法,利用帶參數的構造器注入:

<bean name="a1" class="com.edu.bean.Address"><constructor-arg index="0" type="java.lang.String" value="北京"/><constructor-arg index="1" type="java.lang.String" value="清華"/></bean>

第2種配置方法,利用無參構造器注入:

<bean name="a2" class="com.edu.bean.Address"/>

第3種配置方法,利用屬性的setter方法注入:

<bean name="a3" class="com.edu.bean.Address"><property name="city" value="北京"></property><property name="school" value="清華"></property></bean>

第4種配置方法,利用屬性的setter方法注入引用屬性:

<bean name="addr" class="com.edu.bean.Address"><property name="city" value="北京"></property><property name="school" value="清華"></property></bean><bean name="ss" class="com.edu.bean.Student"><property name="name" value="張三"></property><property name="age" value="20"></property><property name="address" ref="addr"></property></bean>

(三)Spring運算式——SpEL(Spring  Expression  Lanuage)

使用“#{...}”作為定界符。所有在大括弧中的字元都將被認為是SpEL。SpEL為Bean的屬性動態賦值提供了便利。

以下樣本每一組兩條語句均為等價表示:

<property name="count" value="#{5}"></property><property name="count" value="5"></property><property name="address" value="#{addr}"></property><property name="address" ref="addr"></property><property name="address" value="#{addr.city}"></property>

(四)基於註解方式的Bean配置

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.