上一小節(jié)已經(jīng)新建了Gradle模塊用來測(cè)試(如果沒有也沒關(guān)系盟步,不影響接下來的分析,可以直接在Spring的spring-beans
模塊下測(cè)試即可)躏结。接下來回顧一下Spring中的一些知識(shí)點(diǎn)却盘,以便于更好的的分析源碼,本小節(jié)分析一下Spring實(shí)例化bean的三種方式媳拴。
Spring實(shí)例化Bean的方式大致上可以分為三種谷炸,構(gòu)造函數(shù)實(shí)例化,工廠方法實(shí)例化禀挫,靜態(tài)工廠方法實(shí)例化。
1.構(gòu)造函數(shù)實(shí)例化(無參構(gòu)造函數(shù)和有參構(gòu)造函數(shù))
- 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;
/**
* 默認(rèn)構(gòu)造函數(shù)
*/
public Dog() {
}
/**
* 構(gòu)造函數(shù)
* @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">
<!-- ====================實(shí)例化bean的方式Begin==================== -->
<!-- 默認(rèn)構(gòu)造實(shí)例化 -->
<bean id="dog1" class="com.lyc.cn.v2.day01.Dog"/>
<!-- 指定構(gòu)造器實(shí)例化 -->
<bean id="dog2" class="com.lyc.cn.v2.day01.Dog">
<!-- 指定構(gòu)造器參數(shù) index對(duì)應(yīng)構(gòu)造器中參數(shù)的位置 -->
<!-- 也可以通過指定參數(shù)類型拓颓,指定參數(shù)名稱來注入屬性-->
<constructor-arg index="0" value="小明"/>
<constructor-arg index="1" value="3"/>
</bean>
<!-- ====================實(shí)例化bean的方式End==================== -->
</beans>
2.工廠方法
- Factory
package com.lyc.cn.v2.day01;
/**
* 工廠方法實(shí)例化
* @author LiYanChao
* @create: 2018-09-07 23:40
*/
public class DogFactory {
public Dog newInstance(String name, int age) {
return new Dog(name, age);
}
}
- xml
<!-- 實(shí)例工廠方法實(shí)例化 -->
<bean id="dogFactory" class="com.lyc.cn.v2.day01.DogFactory"/>
<!-- 不能指定class屬性,此時(shí)必須使用factory-bean屬性來指定工廠Bean,factory-method屬性指定實(shí)例化Bean的方法 -->
<bean id="dog4" factory-bean="dogFactory" factory-method="newInstance">
<constructor-arg index="0" value="小明"/>
<constructor-arg index="1" value="3"/>
</bean>
3.靜態(tài)工廠方法
- Factory
package com.lyc.cn.v2.day01;
/**
* 靜態(tài)工廠實(shí)例化
* @author LiYanChao
* @create: 2018-09-07 23:40
*/
public class DogStaticFactory {
// 靜態(tài)工廠方法
public static Dog newInstance(String name, int age) {
// 返回需要的Bean實(shí)例
return new Dog(name, age);
}
}
- xml
<!-- 靜態(tài)工廠方法實(shí)例化 -->
<bean id="dog3" class="com.lyc.cn.v2.day01.DogStaticFactory" factory-method="newInstance">
<!-- 指定構(gòu)造器參數(shù) index對(duì)應(yīng)構(gòu)造器中參數(shù)的位置 -->
<constructor-arg index="0" value="小明"/>
<constructor-arg index="1" value="3"/>
</bean>
以上就是實(shí)例化Bean方式的Bean语婴,F(xiàn)actory和xml配置,比較簡單驶睦,而且都有注釋砰左,不一一講解了,接下來新建一個(gè)測(cè)試類场航,看一下運(yùn)行效果缠导。
4. 測(cè)試
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========測(cè)試方法開始=======\n");
xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("v2/day01.xml"));
}
@After
public void after() {
System.out.println("\n========測(cè)試方法結(jié)束=======\n");
}
@Test
public void test1() {
// 默認(rèn)構(gòu)造器
System.out.println("默認(rèn)構(gòu)造器");
Dog dog1 = xmlBeanFactory.getBean("dog1", Dog.class);
dog1.sayHello();
}
@Test
public void test2() {
// 指定構(gòu)造器
System.out.println("有參構(gòu)造器");
Dog dog2 = xmlBeanFactory.getBean("dog2", Dog.class);
dog2.sayHello();
}
@Test
public void test3() {
// 靜態(tài)工廠
System.out.println("靜態(tài)工廠");
Dog dog3 = xmlBeanFactory.getBean("dog3", Dog.class);
dog3.sayHello();
}
@Test
public void test4() {
// 實(shí)例工廠
System.out.println("實(shí)例工廠");
Dog dog4 = xmlBeanFactory.getBean("dog4", Dog.class);
dog4.sayHello();
}
}
5.測(cè)試結(jié)果
========測(cè)試方法開始=======
默認(rèn)構(gòu)造器
大家好, 我叫null, 我今年0歲了
========測(cè)試方法結(jié)束=======
========測(cè)試方法開始=======
有參構(gòu)造器
大家好, 我叫小明, 我今年3歲了
========測(cè)試方法結(jié)束=======
========測(cè)試方法開始=======
靜態(tài)工廠
大家好, 我叫小明, 我今年3歲了
========測(cè)試方法結(jié)束=======
========測(cè)試方法開始=======
實(shí)例工廠
大家好, 我叫小明, 我今年3歲了
========測(cè)試方法結(jié)束=======