Spring學習筆記--Spring-認識Bean

前一篇入門程序後,還是會有疑問,什麼是bean?

Spring 可以被看作一個大型工廠,它的作用就是生產和管理Spring容器中的Bean,如果需要使用它,就要對Spring的配置文件進行配置。

目錄

  1. Bean的配置

    1. XML文件配置
    2. 簡單案例使用properties格式配置文件
  2. Bean的實例化

    1. 構造器實例化
    2. 靜態工廠方式實例化
    3. 實例工廠方式實例化

1. Bean的配置

Spring支持XML和Properites兩種格式的配置文件,常用的就是XML格式配置,這種方式通過XML文件來註冊並管理Bean之間的依賴關係。

1.1 XML文件配置

XML配置文件的根元素是<beans></beans><beans>中包含了多個<bean>子元素,每一個<bean>子元素定義了一個Bean,描述了該Bean如何被裝配到Spring容器中。

  • <bean>元素的常用屬性
屬性 描述
id Bean的唯一標識符
class 指定了Bean的具體實現類(包含該類所在包)
scope 用來設置Bean作用域
init-method Bean初始化方法
destroy-method Bean銷燬方法
factory-bean Bean實例化時使用實例化工廠方式所需屬性
factory-method Bean實例化時使用靜態/實例化工廠方式所需屬性
  • <bean>元素的常用子元素
子元素 描述
constructor-arg 使用此元素傳入構造函數參數進行實例化(構造注入)
property 用於調用Bean實例中的setter方法完成屬性賦值(設值注入)
  • <constructor-arg>的屬性/子元素
屬性/子元素 描述
index(屬性) 指定構造函數的序號(從0開始)
type(屬性) 指定構造參數的類型
ref/value(可作爲子元素,屬性) 指定參數值/指定對Bean工廠中某個Bean實例的引用
ref/value(可作爲子元素,屬性) 指定參數值
  • <property>的屬性/子元素
屬性/子元素 描述
name(屬性) Bean實例中的相應屬性名
ref/value(可作爲子元素,屬性) 指定參數值/指定對Bean工廠中某個Bean實例的引用
ref/value(可作爲子元素,屬性) 指定參數值

1.2 簡單案例使用properties格式配置文件

在Spring不常用,(題外話)但是Spring Boot中不使用XML配置後(用註解替代了),常用的是properties格式的配置文件,下面將通過簡單例子描述

  • 創建Web項目,導入所需jar包
  • 在src下創建application.properties
  • 創建包(我創建的包com.xhh.properties)下創建類(我創建的類Bean4.java,裏面不用寫什麼)
  • 創建xml文件(我創建的Bean4.xml)
  • 創建測試類(我創建的Bean4Test.java)
  • 在xml文件寫入(代碼在下)
  • 在application.properties寫入 bean4=com.xhh.properties.Bean4
  • 運行測試(代碼在下)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	  <!-- 若報錯,檢查beans中有沒有xmlns:context值和
	  http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd -->
      <context:property-placeholder location="classpath:application.properties" />
	  
	  <bean id="bean4" class="${bean4}"></bean>
</beans>
package com.xhh.properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Bean4Test {

	public static void main(String[] args) {
		String beanXmlPath = "com/xhh/properties/Bean4.xml";
		
		ApplicationContext applicationContext = new 
				ClassPathXmlApplicationContext(beanXmlPath);
		
		Bean4 bean = (Bean4) applicationContext.getBean("bean4");
		System.out.println(bean);
	}
}

但還是用了XML,所以Spring中常用的還是XML,而Spring Boot用註解替代了在XML中配置Bean,Spring Boot的魅力。。。

2. Bean的實例化

在面向對象的程序中,如果想要使用某個對象,就要先實例化這個對象,同樣在Spring中想要使用容器中的Bean,也需要實例化Bean。有三種方式:

  • 構造器實例化(常用)
  • 靜態工廠方式實例化
  • 實例工廠方式實例化

2.1 構造器實例化

  • 在項目下創建包(我創建的包com.xhh.constructor)
  • 創建類(我創建的類Bean1,不用寫任何方法)
  • 創建xml文件(我創建的類Bean1.xml)
  • 創建測試類(我創建的類BeanTest1),運行。

代碼如下:

Bean1.java

package com.xhh.constructor;

public class Bean1 {
}

Bean2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bean1" class="com.xhh.constructor.Bean1">
    </bean>

</beans>

Bean1Test.java

package com.xhh.constructor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Bean1Test {
	
	public static void main(String[] args) {
		
		String beanXmlPath = "com/xhh/constructor/Bean1.xml";
		
		ApplicationContext applicationContext = new 
				ClassPathXmlApplicationContext(beanXmlPath);
		
		Bean1 bean = (Bean1) applicationContext.getBean("bean1");
		System.out.println(bean);
	}
}

這裏提一下,在前一篇中我把xml文件命名爲applicationContext.xml並放到src目錄下,所以使用該配置文件時不需要寫其路徑,只需寫名字,而這次我把它放到了自建的包中,就需要指明路徑。。。如代碼中所示

2.2 靜態工廠方式實例化

  • 在項目下創建包(我創建的包com.xhh.static_factory)
  • 創建類1(我創建的類Bean2.java,不用寫任何方法)
  • 創建類2(我創建的類Bean2Factory.java,創建一個靜態方法(createBean)來放回Bean2實例)
  • 創建xml文件(我創建的類Bean2.xml,裏面的Bean配置,需要使用class指定工廠所在路徑,使用factory-method=createBean,指定在工廠類中創建的靜態方法名
  • 創建測試類(我創建的類BeanTest2),運行。

代碼如下:
Bean2.java

package com.xhh.static_factory;

public class Bean2 {

}

Bean2Factory.java

package com.xhh.static_factory;

public class Bean2Factory {

	public static Bean2 createBean() {
		return new Bean2();
	}
}

Bean2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bean2" class="com.xhh.static_factory.Bean2Factory" factory-method="createBean">
    	
    </bean>

</beans>

Bean2Test.java

package com.xhh.static_factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Bean2Test {

	public static void main(String[] args) {
		
		String beanXmlPath = "com/xhh/static_factory/Bean2.xml";
		
		ApplicationContext applicationContext = new 
				ClassPathXmlApplicationContext(beanXmlPath);
		
		Bean2 bean = (Bean2) applicationContext.getBean("bean2");
		System.out.println(bean);
	}

}

2.3 實例工廠方式實例化

  • 在項目下創建包(我創建的包com.xhh.instance_factory)
  • 創建類1(我創建的類Bean3.java,不用寫任何方法)
  • 創建類2(我創建的類InstanceFactory.java,使用createBean()方法創建Bean3對象)
  • 創建xml文件(我創建的類Bean3.xml,首先配置了一個工廠Bean,然後配置了需要實例化的Bean。在需要實例化的Bean添加屬性:factory-bean=“工廠id” factory-method=“createBean”
  • 創建測試類(我創建的類BeanTest3),運行。

Bean3.java

package com.xhh.instance_factory;

public class Bean3 {

}

InstanceFactory.java.java

package com.xhh.instance_factory;

public class InstanceFactory {

	public InstanceFactory() {
		System.out.println("Bean實例化工廠中");
		
	}
	
	private Bean3 createBean() {
		
		return new Bean3();
	}
}

Bean3.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="instanceFactory" class="com.xhh.instance_factory.InstanceFactory">
    </bean>

	<bean id="bean3" factory-bean="instanceFactory" factory-method="createBean"></bean>
</beans>

Bean3Test.java

package com.xhh.instance_factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xhh.static_factory.Bean2;

public class Bean3Test {

	public static void main(String[] args) {
		String beanXmlPath = "com/xhh/instance_factory/Bean3.xml";
		
		ApplicationContext applicationContext = new 
				ClassPathXmlApplicationContext(beanXmlPath);
		
		Bean3 bean = (Bean3) applicationContext.getBean("bean3");
		System.out.println(bean);
	}
}

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