第三章、Spring Bean

第三章、Spring Bean

一、Spring Bean定義

​ 被稱作 bean 的對象是構成應用程序的支柱也是由 Spring IoC 容器管理的。bean 是一個被實例化,組裝,並通過 Spring IoC 容器所管理的對象。這些 bean 是由用容器提供的配置元數據創建的,例如,已經在先前章節看到的,在 XML 的表單中的 定義。

bean 定義包含稱爲配置元數據的信息,下述容器也需要知道配置元數據:

  • 如何創建一個 bean
  • bean 的生命週期的詳細信息
  • bean 的依賴關係

在這裏插入圖片描述

二、Spring Bean屬性:

屬性 描述
class 這個屬性是強制性的,並且指定用來創建 bean 的 bean 類。
name 這個屬性指定唯一的 bean 標識符。在基於 XML 的配置元數據中,你可以使用 ID 和/或 name 屬性來指定 bean 標識符。
scope 這個屬性指定由特定的 bean 定義創建的對象的作用域,它將會在 bean 作用域的章節中進行討論。
constructor-arg 用來注入依賴關係。
properties 用來注入依賴關係。
autowiring mode 用來注入依賴關係。
lazy-initialization mode 延遲初始化的 bean 告訴 IoC 容器在它第一次被請求時,而不是在啓動時去創建一個 bean 實例。
initialization 方法 在 bean 的所有必需的屬性被容器設置之後,調用回調方法。它將會在 bean 的生命週期章節中進行討論。
destruction 方法 當包含該 bean 的容器被銷燬時,使用回調方法。它將會在 bean 的生命週期章節中進行討論。
factory-method 指定創建bean的工廠方法。
factory-bean 指定創建bean的工廠類對象,class屬性將失效。
init-method 創建一個bean之後調用該方法,初始化方法必須是一個無參方法。
destory-method 在銷燬bean之前可以執行指定的方法。

三、從IoC容器讀取bean對象的三種方法

public class HelloService {
	public String sayHello(String name) {
		return "Hello " + name;
	}
}
<bean id="helloService" class="com.tjetc.service.HelloService"></bean>
<bean id="helloService2" class="com.tjetc.service.HelloService"></bean>

(1)容器對象.getBean(“bean的id”),精確定位,需要強制轉換

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloService");

(2)容器對象.getBean(“bean的id”,bean.class),精確定位,不需要強制轉換

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean("helloService",HelloService.class);

(3)容器對象.getBean(bean.class),不需要強制轉換

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean(HelloService.class);

四、Spring實例化bean的三種方法

(1)使用默認的構造方法創建bean對象

<bean id="helloService" class="bean的全路徑名">

容器實例化會調用bean的默認無參數的構造方法

public class HelloService {
	public HelloService() {
		System.out.println("HelloService()構造方法...");
	}
	public String sayHello(String name) {
		return "Hello " + name;
	}
}
<bean id="helloService" class="com.tjetc.service.HelloService"></bean>
public static void main(String[] args) {
		// 創建Ioc容器
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloService helloService = context.getBean("helloService",HelloService.class);
		String hello = helloService.sayHello("張三");
		System.out.println(hello);
	}

HelloService()構造方法...

Hello 張三

(2)靜態工廠創建bean對象

<bean id="helloService" class="工廠的類的全路徑" factory-method="工廠類創建對象的工廠方法">

工廠類:

public class HelloServiceFactory {
	public static HelloService createHelloService() {
		System.out.println("createHelloService() ...");
		return new HelloService();
	}
}

applicationContext.xml:

<bean id="helloService" class="com.tjetc.factory.HelloServiceFactory" factory-method="createHelloService"></bean>
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean("helloService",HelloService.class);
String hello = helloService.sayHello("張三");
System.out.println(hello);

(3)配置工廠類bean

helloService配置 factory-bean=”工廠類bean節點的id的值” factory-method=”工廠類創建對象的方法名”

<bean id="helloServiceFactory" class="com.tjetc.factory.HelloServiceFactory"></bean>
<bean id="helloService" factory-bean="helloServiceFactory" factory-method="createHelloService"></bean>
// 創建Ioc容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = context.getBean("helloService",HelloService.class);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章