1. 程式人生 > >Spring食行記之配置bean

Spring食行記之配置bean

ice 小結 類型 eba 重載 context () classpath close

【bean配置】

在XML文件中通過bean節點來配置bean

1 <!-- 
2        配置bean
3        class: bean的全類名,通過反射的方式在IOC容器中創建bean,所以要求bean中必須有無參的構造器。
4        id:標識容器中的bean,id唯一。 
5 -->
6    <bean id="helloWorld" class="com.hk.beans.HelloWorld">
7        <property name="name" value="Spring"></property>
8
</bean>

id:bean的名稱。

--在IOC容器中必須是唯一的。

--若id沒有指定,Spring自動將對應類名作為bean的名字。

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

【註】:

當bean中不存在無參構造器,存在有參構造器時,運行程序,會出現以下錯誤:

技術分享圖片

【Spring容器】

--ApplicationContext代表容器,但實際上它是一個接口。

--在Spring IOC容器讀取bean配置創建bean實例之前,必須對它進行實例化。只有在容器實例化後,才可以從IOC容器中獲取bean實例並使用。

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

(1)BeanFactory:IOC容器的基本實現。

(2)ApplicationContext:提供了更多的高級特性。是BeanFactory的子接口。

BeanFactory是Spring框架的基礎設施,面向Spring本身;

ApplicationContext面向使用Spring框架的開發者,幾乎所有的應用場合都直接使用ApplicationContext而非底層的BeanFactory

無論使用何種方式,配置文件是相同的。

【ApplicationContext】

1.ApplicationContext的主要實現類:

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

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

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

3.ApplicationContext在初始化上下文的時候就實例化所有單例的bean。

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

【從IOC容器中獲取bean】

1.調用ApplicationContext的getBean()方法。該方法是在ApplicationContext的父接口BeanFactory中定義實現的。

技術分享圖片

由圖中可知,還可以由類型獲取bean。

1 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
2 HelloWorld helloWorld = ctx.getBean(HelloWorld.class); 

缺點!!:如果在IOC容器中此類型不唯一(有兩個或以上相同類型的bean時),會出錯。

---小結:

1 //2. 從IOC容器中獲取Bean實例
2 //方式一:利用id定位到IOC容器中的bean
3 //HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
4 //方式二:利用類型返回IOC容器中的bean,但要求IOC容器中必須只能有一個該類型的bean
5 HelloWorld helloWorld = ctx.getBean(HelloWorld.class); 

【依賴註入的方式】

Spring支持3種依賴註入的方式:

--屬性輸入

--構造器註入

--工廠方式(很少使用,不推薦)

1.屬性註入

(1)概述:屬性註入即通過setter方法註入Bean的屬性值或以來的對象。

(2)屬性註入使用<property>元素,使用name屬性指定Bean的屬性名稱,value屬性或<value>子節點指定屬性值。

(3)屬性註入是實際應用中最常用到的註入方式

1    <bean id="helloWorld" class="com.hk.beans.HelloWorld">
2        <property name="name" value="Spring"></property>
3    </bean>

2.構造方法註入

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

(2)構造器註入在<constructor-arg>元素裏聲明屬性,<constructor-arg>中沒有name屬性

Car.java:

 1 package com.hk.beans;
 2 
 3 public class Car {
 4     private String brand;
 5     private String corp;
 6     private double price;
 7     private int maxSpeed;
 8     
 9     public Car(String brand, String corp, double price) {
10         super();
11         this.brand = brand;
12         this.corp = corp;
13         this.price = price;
14     }
15 
16     @Override
17     public String toString() {
18         return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
19                 + ", maxSpeed=" + maxSpeed + "]";
20     }
21 
22     public Car(String brand, String corp, int maxSpeed) {
23         super();
24         this.brand = brand;
25         this.corp = corp;
26         this.maxSpeed = maxSpeed;
27     }
28 }

配置文件代碼:

 1 <!-- 通過構造方法來配置bean的屬性 -->
 2    <bean id="car" class="com.hk.beans.Car">
 3       <constructor-arg value="BWM" index="0"></constructor-arg>
 4       <constructor-arg value="ShangHai" index="1"></constructor-arg>
 5       <constructor-arg value="100000" type="double"></constructor-arg>
 6    </bean>
 7    
 8    <!-- 使用構造器註入屬性值可以指定參數的位置和參數的類型!以區分重載的構造器! -->
 9    <bean id="car2" class="com.hk.beans.Car">
10       <constructor-arg value="bieke" type="java.lang.String"></constructor-arg>
11       <constructor-arg value="BeiJing" type="java.lang.String"></constructor-arg>
12       <constructor-arg value="150" type="int"></constructor-arg>
13    </bean>

(測試)Main.java:

1         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
2         
3         Car car = (Car) ctx.getBean("car");
4         System.out.println(car);
5         
6         car = (Car) ctx.getBean("car2");
7         System.out.println(car);

運行結果;

技術分享圖片

Spring食行記之配置bean