1. 程式人生 > >Spring學習之旅(四)Spring工作原理再探

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

容器 mxml 實現 span ssp express 16px 部分 做了

上篇博文對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配置

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