天天看點

04--Spring執行個體化Bean的三種方式

上一小節已經建立了Gradle子產品用來測試(如果沒有也沒關系,不影響接下來的分析,可以直接在Spring的

spring-beans

子產品下測試即可)。接下來回顧一下Spring中的一些知識點,以便于更好的的分析源碼,本小節分析一下Spring執行個體化bean的三種方式。

Spring執行個體化Bean的方式大緻上可以分為三種,構造函數執行個體化,工廠方法執行個體化,靜态工廠方法執行個體化。

1.構造函數執行個體化(無參構造函數和有參構造函數)
  • bean
package com.lyc.cn.v2.day01;

/**
 1. @author: LiYanChao
 2. @create: 2018-09-27 14:23
 */
public class Dog {
	/** 姓名 **/
	private String name;

	/** 年齡 **/
	private int age;

	/**
	 * 預設構造函數
	 */
	public Dog() {
	}

	/**
	 * 構造函數
	 * @param name 姓名
	 * @param age  年齡
	 */
	public Dog(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public void sayHello() {
		System.out.println("大家好, 我叫" + getName() + ", 我今年" + getAge() + "歲了");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}
           
  • 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" profile="dev">

	<!-- ====================執行個體化bean的方式Begin==================== -->
	<!-- 預設構造執行個體化 -->
	<bean id="dog1" class="com.lyc.cn.v2.day01.Dog"/>

	<!-- 指定構造器執行個體化 -->
	<bean id="dog2" class="com.lyc.cn.v2.day01.Dog">
		<!-- 指定構造器參數 index對應構造器中參數的位置 -->
		<!-- 也可以通過指定參數類型,指定參數名稱來注入屬性-->
		<constructor-arg index="0" value="小明"/>
		<constructor-arg index="1" value="3"/>
	</bean>
	<!-- ====================執行個體化bean的方式End==================== -->
</beans>
           
2.工廠方法
  • Factory
package com.lyc.cn.v2.day01;

/**
 * 工廠方法執行個體化
 * @author LiYanChao
 * @create: 2018-09-07 23:40
 */
public class DogFactory {

	public Dog newInstance(String name, int age) {
		return new Dog(name, age);
	}
}
           
  • xml
<!-- 執行個體工廠方法執行個體化 -->
<bean id="dogFactory" class="com.lyc.cn.v2.day01.DogFactory"/>
<!-- 不能指定class屬性,此時必須使用factory-bean屬性來指定工廠Bean,factory-method屬性指定執行個體化Bean的方法 -->
<bean id="dog4" factory-bean="dogFactory" factory-method="newInstance">
	<constructor-arg index="0" value="小明"/>
	<constructor-arg index="1" value="3"/>
</bean>
           
3.靜态工廠方法
  • Factory
package com.lyc.cn.v2.day01;

/**
 * 靜态工廠執行個體化
 * @author LiYanChao
 * @create: 2018-09-07 23:40
 */
public class DogStaticFactory {

	// 靜态工廠方法
	public static Dog newInstance(String name, int age) {
		// 傳回需要的Bean執行個體
		return new Dog(name, age);
	}
}
           
  • xml
<!-- 靜态工廠方法執行個體化 -->
<bean id="dog3" class="com.lyc.cn.v2.day01.DogStaticFactory" factory-method="newInstance">
	<!-- 指定構造器參數 index對應構造器中參數的位置 -->
	<constructor-arg index="0" value="小明"/>
	<constructor-arg index="1" value="3"/>
</bean>
           

以上就是執行個體化Bean方式的Bean,Factory和xml配置,比較簡單,而且都有注釋,不一一講解了,接下來建立一個測試類,看一下運作效果。

4. 測試
package com.lyc.cn.v2.day01;

import com.lyc.cn.v2.day01.inner.Outer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
 * @author: LiYanChao
 * @create: 2018-09-07 23:40
 */
public class MyTest {
	private XmlBeanFactory xmlBeanFactory;

	@Before
	public void initXmlBeanFactory() {
		System.setProperty("spring.profiles.active", "dev");
		System.out.println("\n========測試方法開始=======\n");
		xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("v2/day01.xml"));
	}

	@After
	public void after() {
		System.out.println("\n========測試方法結束=======\n");
	}

	@Test
	public void test1() {
		// 預設構造器
		System.out.println("預設構造器");
		Dog dog1 = xmlBeanFactory.getBean("dog1", Dog.class);
		dog1.sayHello();
	}

	@Test
	public void test2() {
		// 指定構造器
		System.out.println("有參構造器");
		Dog dog2 = xmlBeanFactory.getBean("dog2", Dog.class);
		dog2.sayHello();
	}

	@Test
	public void test3() {
		// 靜态工廠
		System.out.println("靜态工廠");
		Dog dog3 = xmlBeanFactory.getBean("dog3", Dog.class);
		dog3.sayHello();
	}

	@Test
	public void test4() {
		// 執行個體工廠
		System.out.println("執行個體工廠");
		Dog dog4 = xmlBeanFactory.getBean("dog4", Dog.class);
		dog4.sayHello();
	}
}
           
5.測試結果
========測試方法開始=======

預設構造器
大家好, 我叫null, 我今年0歲了

========測試方法結束=======


========測試方法開始=======

有參構造器
大家好, 我叫小明, 我今年3歲了

========測試方法結束=======


========測試方法開始=======

靜态工廠
大家好, 我叫小明, 我今年3歲了

========測試方法結束=======


========測試方法開始=======

執行個體工廠
大家好, 我叫小明, 我今年3歲了

========測試方法結束=======
           

繼續閱讀