spring專題---第一部分IOC(三)

上一節我們總結了spring IOC的一些特性,包括spring bean的作用域,spring的繼承,依賴,以及spring如何讀取外部資源,spring的p命名空間。如果還有疑惑的朋友都可以下方留言或與我私信哦,我看到後都會一一回復的,我們共同交流,共同進步。
這一節我們總結spring IOC 工廠方法創建對象以及spring IOC自動裝載
第一節:spring IOC工廠方法
IOC是典型的工廠模式,下面我們就來學習如何使用工廠模式來創建bean,IOC 通過工廠模式創建bean有兩種方式:
靜態工廠方法
實例工廠方法
按照慣例,我們依舊通過代碼示例一起學習工廠方法,我們先來學習靜態工廠方法
(1)創建Car實體類

public class Car {
    private int num;
    private String name;
    public Car(int num,String name){
        super();
        this.num=num;
        this.name=name;
    }
    //在此我們省略了toString方法

(2)創建靜態工廠類,靜態工廠方法

public class CarFactory {
    private static Map<Integer,Car> cars;
    static{
        cars=new HashMap<Integer,Car>();
        cars.put(1,new Car(1,"奔馳"));
        cars.put(2,new Car(2,"奧迪"));
    }
    public static Car getCar(int id){
        return cars.get(id);
    }
}

(3)在spring.xml中配置靜態工廠

   <bean id="car1" class="entity.CarFactory" factory-method="getCar">
        <constructor-arg value="1"></constructor-arg>
    </bean>
    <!--factory-method指向的是靜態方法;constructor-arg的value屬性爲調用靜態方法所傳的參數-->

(4)在測試類中獲取car1對象

public class ApplicationContext {
    public static void main(String[] args){
        AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Car car=(Car)applicationContext.getBean("car1");
        System.out.println(car);
    }
}

運行結果:
在這裏插入圖片描述
實例工廠方法
(1)創建實例工廠類,工廠方法

public class InstanceCarFactory {
    private Map<Integer,Car> cars;
    public InstanceCarFactory(){
        cars=new HashMap<Integer, Car>();
        cars.put(1,new Car(1,"奔馳"));
        cars.put(2,new Car(2,"奧迪"));
    }
    public Car getCar(int id){
        return cars.get(id);
    }
}

(2)spring.xml中配置bean

   <bean id="carFactory" class="entity.InstanceCarFactory"></bean>
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="2"></constructor-arg>
    </bean>

(3)在測試類中獲取car2對象

public class ApplicationContext {
    public static void main(String[] args){
        AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Car car=(Car)applicationContext.getBean("car2");
        System.out.println(car);
    }
}

運行結果:
在這裏插入圖片描述
通過上邊兩種工廠方式創建的對象,我們可以看出,靜態工廠方法的方式創建car對象,不需要實例化工廠對象,通過spring.xml我們也可以看出,我們僅僅配置了Car bean,沒有配置工廠 bean。因爲靜態工廠的靜態方法,不需要創建對象即可調用。
而實例工廠創建Car對象,我們必須先實例化工廠對象,因爲調用的是非靜態方法,必須通過工廠對象調用,不能通過類來調用。所以spring.xml中先配置了工廠bean,再配置Car bean。
簡單而言,靜態工廠方式是工廠類中爲靜態方法,實例工廠方式是工廠類中爲非靜態方法。
第二節:spring IOC自動裝載
自動裝載其實和依賴注入有點相似,我們前邊學到依賴注入,A類中包含B類,那麼我們可以通過property的ref將B類注入到A類中。自動裝載是什麼呢?簡單來說就是A類中包含B類,我們可以在bean中使用autowire將B類自動裝載到A類中。
其中autowire有兩個重要屬性值,分別是byName和byType,下面我們就通過一些簡單的例子講解一下這兩個屬性值的用法:
byName:

        <!--spring.xml-->>
        <!--設置了bean標籤autowire屬性,屬性值設爲byName,在這裏IOC容器能通通過id值將bean進行匹配,然後創建對象由API調用-->>
        <bean id="stu" class="entity.Student" autowire="byName">
            <property name="id" value="1"></property>
            <property name="name" value="jacob"></property>
            <property name="age" value="18"></property>
        </bean>
        <bean id="classes" class="entity.Classes">
            <property name="class_name" value="jacob_classes"></property>
            <property name="class_id" value="1001"></property>
        </bean>
//Student.class
public class Student {
    private int id;
    private String name;
    private int age;
    private Classes classes;
//Classes.class
public class Classes {
    private int class_id;
    private String class_name;
//測試類
public class ApplicationContext {
    public static void main(String[] args){
        AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Student student=(Student)applicationContext.getBean("stu");
        System.out.println(student);
    }
}

運行結果:
在這裏插入圖片描述
我們可以看到,我們在id爲stu的bean中添加了autowire,並將其值設爲byName後,生成的對象student中Classes屬性值正是我們配置給id爲classes的屬性值,說明id爲stu的bean是通過byName(bean的id值)匹配到classes並實行自動裝載的。
那麼我們來看一下autowire的另一個值byType:
我們可以將它和API獲取對象的方式放在一起來記憶,不知道小夥伴們還記不記得application是通過什麼方式獲取bean的,一種是id,一種是運行時類,其實自動裝載和它極爲相似。

        <!--spring.xml-->>
        <!--bean的autowire值設置爲byType,IOC通過Student中屬性Classes(entity.Classes)和bean中其他class匹配-->>
        <bean id="stu" class="entity.Student" autowire="byType">
            <property name="id" value="1"></property>
            <property name="name" value="jacob"></property>
            <property name="age" value="18"></property>
        </bean>
        <bean id="classes" class="entity.Classes">
            <property name="class_name" value="jacob_classes"></property>
            <property name="class_id" value="1001"></property>
        </bean>

運行結果:
在這裏插入圖片描述
如果我在spring.xml中設置兩個bean,它們都是類Classes的bean,我們再來看一下結果:

        <bean id="stu" class="entity.Student" autowire="byType">
            <property name="id" value="1"></property>
            <property name="name" value="jacob"></property>
            <property name="age" value="18"></property>
        </bean>
        <bean id="classes" class="entity.Classes">
            <property name="class_name" value="jacob_classes"></property>
            <property name="class_id" value="1001"></property>
        </bean>

        <bean id="classes01" class="entity.Classes">
            <property name="class_name" value="jacob_clsses01"></property>
            <property name="class_id" value="1002"></property>
        </bean>

運行結果:
在這裏插入圖片描述
我們看到,程序報錯了,原因是在xml配置中,我們配置了兩個相同class的bean,這也證明了屬性byType就是通過class進行匹配的。
總結:
這一節我們總結了spring IOC通過工廠模式創建對象,包括靜態工廠方式和實例工廠方式,其中靜態工廠方式創建對象時,我們在spring.xml中只需配置需要創建對象的bean,因爲靜態工廠類中均爲靜態方法,可以直接用類來調用,而實例工廠方式創建對象需要配置工廠類bean和需要創建對象的類bean。接着我們講解了IOC中另一個特性,自動裝載,它和依賴注入極爲相似,不同的是,它需要配置在bean標籤頭中,有兩種屬性值,分別爲byName和byType,前者通過id匹配,後者通過運行時類匹配,但是需要注意的是,使用byType時,spring.xml中只能配置一種運行時類的bean,配置多個程序會報錯。
以上便是我對這一部分的理解,如果有錯誤或者你有其他疑惑都可以留言給出,我都會一一進行回覆,希望對你有所幫助,如果寫的不好也請您多多包涵。歡迎在下方補充留言喲。
對SSM框架感興趣的童鞋,可以移步這裏,在這裏你可以快速的搭建好一個SSM框架。
如果你在寫項目的時候,遇到一些不尋常的問題,也可以關注我的博客園,上邊會發布一些我在寫項目中遇到的各種問題以及解決方式。

發佈了31 篇原創文章 · 獲贊 30 · 訪問量 6515
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章