關(guān)于Spring Bean實(shí)例過程中使用反射和遞歸處理的Bean屬性填充問題
一、前言
超賣、掉單、冪等,你的程序總是不抗揍!
想想,運(yùn)營(yíng)已經(jīng)對(duì)外宣傳了七八天的活動(dòng),滿心歡喜的等著最后一天頁面上線對(duì)外了,突然出現(xiàn)了一堆異常、資損、閃退,而用戶流量稍縱即逝,最后想死的心都有!
就編程開發(fā)來講,丟三落四、亂碼七糟,可能這就是大部分初級(jí)程序員日常開發(fā)的真實(shí)寫照,在即使有測(cè)試人員驗(yàn)證的情況下,也會(huì)出現(xiàn)帶Bug上線的現(xiàn)象,只不過是當(dāng)時(shí)沒有發(fā)現(xiàn)而已!因?yàn)槭侨藢懘a,就一定會(huì)有錯(cuò)誤,即使是老碼農(nóng)
就程序Bug來講,會(huì)包括產(chǎn)品PRD流程上的Bug、運(yùn)營(yíng)配置活動(dòng)時(shí)候的Bug、研發(fā)開發(fā)時(shí)功能實(shí)現(xiàn)的Bug、測(cè)試驗(yàn)證時(shí)漏掉流程的Bug、上線過程中運(yùn)維服務(wù)相關(guān)配置的Bug,而這些其實(shí)都可以通過制定的流程規(guī)范和一定的研發(fā)經(jīng)驗(yàn)積累,慢慢盡可能減少。
而另外一類是溝通留下的Bug,通常情況下業(yè)務(wù)提需求、產(chǎn)品定方案、研發(fā)做實(shí)現(xiàn),最終還要有UI、測(cè)試、運(yùn)營(yíng)、架構(gòu)等等各個(gè)環(huán)節(jié)的人員參與到一個(gè)項(xiàng)目的承接、開發(fā)到上線運(yùn)行,而在這一群人需要保持一個(gè)統(tǒng)一的信息傳播其實(shí)是很難的。比如在項(xiàng)目開發(fā)中期,運(yùn)營(yíng)給產(chǎn)品說了一個(gè)新增的需求,產(chǎn)品覺得功能也不大,隨即找到對(duì)應(yīng)的前端研發(fā)加個(gè)邏輯,但沒想到可能也影響到了后端的開發(fā)和測(cè)試的用例。最后功能雖然是上線了,可并不在整個(gè)產(chǎn)研測(cè)的需求覆蓋度范圍里,也就隱形的埋下了一個(gè)坑。
所以,如果你想讓你的程序很抗揍,接的住農(nóng)夫三拳,那么你要做的就不只是一個(gè)單純的搬磚碼農(nóng)!
二、目標(biāo)
首先我們回顧下這幾章節(jié)都完成了什么,包括:實(shí)現(xiàn)一個(gè)容器、定義和注冊(cè)Bean、實(shí)例化Bean,按照是否包含構(gòu)造函數(shù)實(shí)現(xiàn)不同的實(shí)例化策略,那么在創(chuàng)建對(duì)象實(shí)例化這我們還缺少什么?其實(shí)還缺少一個(gè)關(guān)于類中是否有屬性的問題,如果有類中包含屬性那么在實(shí)例化的時(shí)候就需要把屬性信息填充上,這樣才是一個(gè)完整的對(duì)象創(chuàng)建。
對(duì)于屬性的填充不只是 int、Long、String,還包括還沒有實(shí)例化的對(duì)象屬性,都需要在 Bean 創(chuàng)建時(shí)進(jìn)行填充操作。不過這里我們暫時(shí)不會(huì)考慮 Bean 的循環(huán)依賴,否則會(huì)把整個(gè)功能實(shí)現(xiàn)撐大,這樣新人學(xué)習(xí)時(shí)就把握不住了,待后續(xù)陸續(xù)先把核心功能實(shí)現(xiàn)后,再逐步完善
三、設(shè)計(jì)
鑒于屬性填充是在 Bean 使用 newInstance 或者 Cglib 創(chuàng)建后,開始補(bǔ)全屬性信息,那么就可以在類 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加補(bǔ)全屬性方法。這部分大家在實(shí)習(xí)的過程中也可以對(duì)照Spring源碼學(xué)習(xí),這里的實(shí)現(xiàn)也是Spring的簡(jiǎn)化版,后續(xù)對(duì)照學(xué)習(xí)會(huì)更加易于理解

- 屬性填充要在類實(shí)例化創(chuàng)建之后,也就是需要在
AbstractAutowireCapableBeanFactory的 createBean 方法中添加applyPropertyValues操作。 - 由于我們需要在創(chuàng)建Bean時(shí)候填充屬性操作,那么就需要在 bean 定義 BeanDefinition 類中,添加 PropertyValues 信息。
- 另外是填充屬性信息還包括了 Bean 的對(duì)象類型,也就是需要再定義一個(gè) BeanReference,里面其實(shí)就是一個(gè)簡(jiǎn)單的 Bean 名稱,在具體的實(shí)例化操作時(shí)進(jìn)行遞歸創(chuàng)建和填充,與 Spring 源碼實(shí)現(xiàn)一樣。Spring 源碼中 BeanReference 是一個(gè)接口
四、實(shí)現(xiàn)
1. 工程結(jié)構(gòu)
small-spring-step-04
└── src
├── main
│ └── java
│ └── cn.bugstack.springframework.beans
│ ├── factory
│ │ ├── factory
│ │ │ ├── BeanDefinition.java
│ │ │ ├── BeanReference.java
│ │ │ └── SingletonBeanRegistry.java
│ │ ├── support
│ │ │ ├── AbstractAutowireCapableBeanFactory.java
│ │ │ ├── AbstractBeanFactory.java
│ │ │ ├── BeanDefinitionRegistry.java
│ │ │ ├── CglibSubclassingInstantiationStrategy.java
│ │ │ ├── DefaultListableBeanFactory.java
│ │ │ ├── DefaultSingletonBeanRegistry.java
│ │ │ ├── InstantiationStrategy.java
│ │ │ └── SimpleInstantiationStrategy.java
│ │ └── BeanFactory.java
│ ├── BeansException.java
│ ├── PropertyValue.java
│ └── PropertyValues.java
└── test
└── java
└── cn.bugstack.springframework.test
├── bean
│ ├── UserDao.java
│ └── UserService.java
└── ApiTest.java
工程源碼:
《Spring 手?jǐn)]專欄》學(xué)習(xí)源碼介紹
專欄地址:https://bugstack.cn/itstack/spring.html
源碼地址:https://github.com/fuzhengwei/small-spring
Spring Bean 容器類關(guān)系,如圖 5-2

- 本章節(jié)中需要新增加3個(gè)類,
BeanReference(類引用)、PropertyValue(屬性值)、PropertyValues(屬性集合),分別用于類和其他類型屬性填充操作。 - 另外改動(dòng)的類主要是
AbstractAutowireCapableBeanFactory,在 createBean 中補(bǔ)全屬性填充部分。
2. 定義屬性
cn.bugstack.springframework.beans.PropertyValue
public class PropertyValue {
private final String name;
private final Object value;
public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
}
// ...get/set
}
cn.bugstack.springframework.beans.PropertyValues
public class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<>();
public void addPropertyValue(PropertyValue pv) {
this.propertyValueList.add(pv);
}
public PropertyValue[] getPropertyValues() {
return this.propertyValueList.toArray(new PropertyValue[0]);
}
public PropertyValue getPropertyValue(String propertyName) {
for (PropertyValue pv : this.propertyValueList) {
if (pv.getName().equals(propertyName)) {
return pv;
}
}
return null;
}
}
這兩個(gè)類的作用就是創(chuàng)建出一個(gè)用于傳遞類中屬性信息的類,因?yàn)閷傩钥赡軙?huì)有很多,所以還需要定義一個(gè)集合包裝下。
3. Bean定義補(bǔ)全
cn.bugstack.springframework.beans.factory.config.BeanDefinition
public class BeanDefinition {
private Class beanClass;
private PropertyValues propertyValues;
public BeanDefinition(Class beanClass) {
this.beanClass = beanClass;
this.propertyValues = new PropertyValues();
}
public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
this.beanClass = beanClass;
this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
}
// ...get/set
}
- 在 Bean 注冊(cè)的過程中是需要傳遞 Bean 的信息,在幾個(gè)前面章節(jié)的測(cè)試中都有所體現(xiàn)
new BeanDefinition(UserService.class, propertyValues); - 所以為了把屬性一定交給 Bean 定義,所以這里填充了 PropertyValues 屬性,同時(shí)把兩個(gè)構(gòu)造函數(shù)做了一些簡(jiǎn)單的優(yōu)化,避免后面 for 循環(huán)時(shí)還得判斷屬性填充是否為空。
4. Bean 屬性填充
cn.bugstack.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 給 Bean 填充屬性
applyPropertyValues(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName, bean);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
}
/**
* Bean 屬性填充
*/
protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
try {
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
String name = propertyValue.getName();
Object value = propertyValue.getValue();
if (value instanceof BeanReference) {
// A 依賴 B,獲取 B 的實(shí)例化
BeanReference beanReference = (BeanReference) value;
value = getBean(beanReference.getBeanName());
}
// 屬性填充
BeanUtil.setFieldValue(bean, name, value);
}
} catch (Exception e) {
throw new BeansException("Error setting property values:" + beanName);
}
}
public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
this.instantiationStrategy = instantiationStrategy;
}
}
- 這個(gè)類的內(nèi)容稍微有點(diǎn)長(zhǎng),主要包括三個(gè)方法:createBean、createBeanInstance、applyPropertyValues,這里我們主要關(guān)注 createBean 的方法中調(diào)用的 applyPropertyValues 方法。
- 在 applyPropertyValues 中,通過獲取
beanDefinition.getPropertyValues()循環(huán)進(jìn)行屬性填充操作,如果遇到的是 BeanReference,那么就需要遞歸獲取 Bean 實(shí)例,調(diào)用 getBean 方法。 - 當(dāng)把依賴的 Bean 對(duì)象創(chuàng)建完成后,會(huì)遞歸回現(xiàn)在屬性填充中。這里需要注意我們并沒有去處理循環(huán)依賴的問題,這部分內(nèi)容較大,后續(xù)補(bǔ)充。BeanUtil.setFieldValue(bean, name, value) 是 hutool-all 工具類中的方法,你也可以自己實(shí)現(xiàn)
五、測(cè)試
1. 事先準(zhǔn)備
cn.bugstack.springframework.test.bean.UserDao
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "小傅哥");
hashMap.put("10002", "八杯水");
hashMap.put("10003", "阿毛");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
cn.bugstack.springframework.test.bean.UserService
public class UserService {
private String uId;
private UserDao userDao;
public void queryUserInfo() {
System.out.println("查詢用戶信息:" + userDao.queryUserName(uId));
}
// ...get/set
}
Dao、Service,是我們平常開發(fā)經(jīng)常使用的場(chǎng)景。在 UserService 中注入 UserDao,這樣就能體現(xiàn)出Bean屬性的依賴了。
2. 測(cè)試用例
@Test
public void test_BeanFactory() {
// 1.初始化 BeanFactory
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 2. UserDao 注冊(cè)
beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));
// 3. UserService 設(shè)置屬性[uId、userDao]
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(new PropertyValue("uId", "10001"));
propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao")));
// 4. UserService 注入bean
BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues);
beanFactory.registerBeanDefinition("userService", beanDefinition);
// 5. UserService 獲取bean
UserService userService = (UserService) beanFactory.getBean("userService");
userService.queryUserInfo();
}
- 與直接獲取 Bean 對(duì)象不同,這次我們還需要先把 userDao 注入到 Bean 容器中。
beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class)); - 接下來就是屬性填充的操作了,一種是普通屬性
new PropertyValue("uId", "10001"),另外一種是對(duì)象屬性new PropertyValue("userDao",new BeanReference("userDao")) - 接下來的操作就簡(jiǎn)單了,只不過是正常獲取 userService 對(duì)象,調(diào)用方法即可。
3. 測(cè)試結(jié)果
查詢用戶信息:小傅哥 Process finished with exit code 0
- 從測(cè)試結(jié)果看我們的屬性填充已經(jīng)起作用了,因?yàn)橹挥袑傩蕴畛浜?,才能調(diào)用到Dao方法,如:
userDao.queryUserName(uId) - 那么我們?cè)诳纯碊ebug調(diào)試的情況下,有沒有進(jìn)入到實(shí)現(xiàn)的 Bean 屬性填充中,如下:

好,就是截圖這里,我們看到已經(jīng)開始進(jìn)行屬性填充操作了,當(dāng)發(fā)現(xiàn)屬性是 BeanReference 時(shí),則需要獲取創(chuàng)建 Bean 實(shí)例。
六、總結(jié)
- 在本章節(jié)中我們把 AbstractAutowireCapableBeanFactory 類中的創(chuàng)建對(duì)象功能又做了擴(kuò)充,依賴于是否有構(gòu)造函數(shù)的實(shí)例化策略完成后,開始補(bǔ)充 Bean 屬性信息。當(dāng)遇到 Bean 屬性為 Bean 對(duì)象時(shí),需要遞歸處理。最后在屬性填充時(shí)需要用到反射操作,也可以使用一些工具類處理。
- 每一個(gè)章節(jié)的功能點(diǎn)我們都在循序漸進(jìn)的實(shí)現(xiàn),這樣可以讓新人更好的接受關(guān)于 Spring 中的設(shè)計(jì)思路。尤其是在一些已經(jīng)開發(fā)好的類上,怎么擴(kuò)充新的功能時(shí)候的設(shè)計(jì)更為重要。學(xué)習(xí)編程有的時(shí)候?qū)W習(xí)思路設(shè)計(jì)要比僅僅是做簡(jiǎn)單實(shí)現(xiàn),更能提升編程思維。
- 到這一章節(jié)關(guān)于 Bean 的創(chuàng)建操作就開發(fā)完成了,接下來需要整個(gè)框架的基礎(chǔ)上完成資源屬性的加載,就是我們需要去動(dòng) Xml 配置了,讓我們這小框架越來越像 Spring。另外在框架實(shí)現(xiàn)的過程中所有的類名都會(huì)參考 Spring 源碼,以及相應(yīng)的設(shè)計(jì)實(shí)現(xiàn)步驟也是與 Spring 源碼中對(duì)應(yīng),只不過會(huì)簡(jiǎn)化一些流程,但你可以拿相同的類名,去搜到每一個(gè)功能在 Spring 源碼中的實(shí)現(xiàn)。
以上就是關(guān)于Spring Bean實(shí)例過程中使用反射和遞歸處理的Bean屬性填充問題的詳細(xì)內(nèi)容,更多關(guān)于Spring Bean Bean屬性填充的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot控制層傳遞參數(shù)為非必填值的操作
這篇文章主要介紹了springboot控制層傳遞參數(shù)為非必填值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
如何解決java:找不到符號(hào)符號(hào):類__(使用了lombok的注解)
在使用IntelliJ IDEA開發(fā)Java項(xiàng)目時(shí),可能遇到通過@lombok注解自動(dòng)生成get和set方法不生效的問題,解決這一問題需要幾個(gè)步驟,首先,確認(rèn)Lombok插件已在IDEA中安裝并啟用,其次,確保項(xiàng)目中已添加Lombok的依賴,對(duì)于Maven和Gradle項(xiàng)目2024-10-10
Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問題
這篇文章主要介紹了Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問題的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
解決子線程無法訪問父線程中通過ThreadLocal設(shè)置的變量問題
這篇文章主要介紹了解決子線程無法訪問父線程中通過ThreadLocal設(shè)置的變量問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式
這篇文章主要介紹了Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

