04--Spring例項化Bean的三種方式
阿新 • • 發佈:2018-12-18
上一小節已經新建了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歲了
========測試方法結束=======