自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一鳴驚人,為Bean對(duì)象注入屬性和依賴Bean的功能實(shí)現(xiàn)

開發(fā) 前端
鑒于屬性填充是在 Bean 使用 newInstance 或者 Cglib 創(chuàng)建后,開始補(bǔ)全屬性信息,那么就可以在類 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加補(bǔ)全屬性方法。

[[403395]]

本文轉(zhuǎn)載自微信公眾號(hào)「bugstack蟲洞?!梗髡咝「蹈?。轉(zhuǎn)載本文請聯(lián)系bugstack蟲洞棧公眾號(hào)。

目錄

  • 一、前言
  • 二、目標(biāo)
  • 三、設(shè)計(jì)
  • 四、實(shí)現(xiàn)
    • 1. 工程結(jié)構(gòu)
    • 2. 定義屬性
    • 3. Bean定義補(bǔ)全
    • 4. Bean 屬性填充
  • 五、測試
    • 1. 事先準(zhǔn)備
    • 2. 測試用例
    • 3. 測試結(jié)果
  • 六、總結(jié)
  • 七、系列推薦

一、前言

超賣、掉單、冪等,你的程序總是不抗揍!

想想,運(yùn)營已經(jīng)對(duì)外宣傳了七八天的活動(dòng),滿心歡喜的等著最后一天頁面上線對(duì)外了,突然出現(xiàn)了一堆異常、資損、閃退,而用戶流量稍縱即逝,最后想死的心都有!

就編程開發(fā)來講,丟三落四、亂碼七糟,可能這就是大部分初級(jí)程序員日常開發(fā)的真實(shí)寫照,在即使有測試人員驗(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)營配置活動(dòng)時(shí)候的Bug、研發(fā)開發(fā)時(shí)功能實(shí)現(xiàn)的Bug、測試驗(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、測試、運(yùn)營、架構(gòu)等等各個(gè)環(huán)節(jié)的人員參與到一個(gè)項(xiàng)目的承接、開發(fā)到上線運(yùn)行,而在這一群人需要保持一個(gè)統(tǒng)一的信息傳播其實(shí)是很難的。比如在項(xiàng)目開發(fā)中期,運(yùn)營給產(chǎn)品說了一個(gè)新增的需求,產(chǎn)品覺得功能也不大,隨即找到對(duì)應(yīng)的前端研發(fā)加個(gè)邏輯,但沒想到可能也影響到了后端的開發(fā)和測試的用例。最后功能雖然是上線了,可并不在整個(gè)產(chǎn)研測的需求覆蓋度范圍里,也就隱形的埋下了一個(gè)坑。

所以,如果你想讓你的程序很抗揍,接的住農(nóng)夫三拳,那么你要做的就不只是一個(gè)單純的搬磚碼農(nóng)!

二、目標(biāo)

首先我們回顧下這幾章節(jié)都完成了什么,包括:實(shí)現(xiàn)一個(gè)容器、定義和注冊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的簡化版,后續(xù)對(duì)照學(xué)習(xí)會(huì)更加易于理解

屬性填充要在類實(shí)例化創(chuàng)建之后,也就是需要在 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加 applyPropertyValues 操作。

由于我們需要在創(chuàng)建Bean時(shí)候填充屬性操作,那么就需要在 bean 定義 BeanDefinition 類中,添加 PropertyValues 信息。

另外是填充屬性信息還包括了 Bean 的對(duì)象類型,也就是需要再定義一個(gè) BeanReference,里面其實(shí)就是一個(gè)簡單的 Bean 名稱,在具體的實(shí)例化操作時(shí)進(jìn)行遞歸創(chuàng)建和填充,與 Spring 源碼實(shí)現(xiàn)一樣。Spring 源碼中 BeanReference 是一個(gè)接口

四、實(shí)現(xiàn)

1. 工程結(jié)構(gòu)

  1. small-spring-step-04 
  2. └── src 
  3.     ├── main 
  4.     │   └── java 
  5.     │       └── cn.bugstack.springframework.beans 
  6.     │           ├── factory 
  7.     │           │   ├── factory 
  8.     │           │   │   ├── BeanDefinition.java 
  9.     │           │   │   ├── BeanReference.java 
  10.     │           │   │   └── SingletonBeanRegistry.java 
  11.     │           │   ├── support 
  12.     │           │   │   ├── AbstractAutowireCapableBeanFactory.java 
  13.     │           │   │   ├── AbstractBeanFactory.java 
  14.     │           │   │   ├── BeanDefinitionRegistry.java 
  15.     │           │   │   ├── CglibSubclassingInstantiationStrategy.java 
  16.     │           │   │   ├── DefaultListableBeanFactory.java 
  17.     │           │   │   ├── DefaultSingletonBeanRegistry.java 
  18.     │           │   │   ├── InstantiationStrategy.java 
  19.     │           │   │   └── SimpleInstantiationStrategy.java 
  20.     │           │   └── BeanFactory.java 
  21.     │           ├── BeansException.java 
  22.     │           ├── PropertyValue.java 
  23.     │           └── PropertyValues.java 
  24.     └── test 
  25.         └── java 
  26.             └── cn.bugstack.springframework.test 
  27.                 ├── bean 
  28.                 │   ├── UserDao.java 
  29.                 │   └── UserService.java 
  30.                 └── ApiTest.java 

工程源碼:公眾號(hào)「bugstack蟲洞棧」,回復(fù):Spring 專欄,獲取完整源碼

Spring Bean 容器類關(guān)系,如圖 5-2

圖 5-2

  • 本章節(jié)中需要新增加3個(gè)類,BeanReference(類引用)、PropertyValue(屬性值)、PropertyValues(屬性集合),分別用于類和其他類型屬性填充操作。
  • 另外改動(dòng)的類主要是 AbstractAutowireCapableBeanFactory,在 createBean 中補(bǔ)全屬性填充部分。

2. 定義屬性

cn.bugstack.springframework.beans.PropertyValue

  1. public class PropertyValue { 
  2.  
  3.     private final String name
  4.  
  5.     private final Object value; 
  6.  
  7.     public PropertyValue(String name, Object value) { 
  8.         this.name = name
  9.         this.value = value; 
  10.     } 
  11.      
  12.     // ...get/set 

cn.bugstack.springframework.beans.PropertyValues

  1. public class PropertyValues { 
  2.  
  3.     private final List<PropertyValue> propertyValueList = new ArrayList<>(); 
  4.  
  5.     public void addPropertyValue(PropertyValue pv) { 
  6.         this.propertyValueList.add(pv); 
  7.     } 
  8.  
  9.     public PropertyValue[] getPropertyValues() { 
  10.         return this.propertyValueList.toArray(new PropertyValue[0]); 
  11.     } 
  12.  
  13.     public PropertyValue getPropertyValue(String propertyName) { 
  14.         for (PropertyValue pv : this.propertyValueList) { 
  15.             if (pv.getName().equals(propertyName)) { 
  16.                 return pv; 
  17.             } 
  18.         } 
  19.         return null
  20.     } 
  21.  

這兩個(gè)類的作用就是創(chuàng)建出一個(gè)用于傳遞類中屬性信息的類,因?yàn)閷傩钥赡軙?huì)有很多,所以還需要定義一個(gè)集合包裝下。

3. Bean定義補(bǔ)全

cn.bugstack.springframework.beans.factory.config.BeanDefinition

  1. public class BeanDefinition { 
  2.  
  3.     private Class beanClass; 
  4.  
  5.     private PropertyValues propertyValues; 
  6.  
  7.     public BeanDefinition(Class beanClass) { 
  8.         this.beanClass = beanClass; 
  9.         this.propertyValues = new PropertyValues(); 
  10.     } 
  11.  
  12.     public BeanDefinition(Class beanClass, PropertyValues propertyValues) { 
  13.         this.beanClass = beanClass; 
  14.         this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues(); 
  15.     } 
  16.      
  17.     // ...get/set 
  • 在 Bean 注冊的過程中是需要傳遞 Bean 的信息,在幾個(gè)前面章節(jié)的測試中都有所體現(xiàn) new BeanDefinition(UserService.class, propertyValues);
  • 所以為了把屬性一定交給 Bean 定義,所以這里填充了 PropertyValues 屬性,同時(shí)把兩個(gè)構(gòu)造函數(shù)做了一些簡單的優(yōu)化,避免后面 for 循環(huán)時(shí)還得判斷屬性填充是否為空。

4. Bean 屬性填充

cn.bugstack.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

  1. public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory { 
  2.  
  3.     private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy(); 
  4.  
  5.     @Override 
  6.     protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException { 
  7.         Object bean = null
  8.         try { 
  9.             bean = createBeanInstance(beanDefinition, beanName, args); 
  10.             // 給 Bean 填充屬性 
  11.             applyPropertyValues(beanName, bean, beanDefinition); 
  12.         } catch (Exception e) { 
  13.             throw new BeansException("Instantiation of bean failed", e); 
  14.         } 
  15.  
  16.         addSingleton(beanName, bean); 
  17.         return bean; 
  18.     } 
  19.  
  20.     protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) { 
  21.         Constructor constructorToUse = null
  22.         Class<?> beanClass = beanDefinition.getBeanClass(); 
  23.         Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors(); 
  24.         for (Constructor ctor : declaredConstructors) { 
  25.             if (null != args && ctor.getParameterTypes().length == args.length) { 
  26.                 constructorToUse = ctor; 
  27.                 break; 
  28.             } 
  29.         } 
  30.         return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args); 
  31.     } 
  32.  
  33.     /** 
  34.      * Bean 屬性填充 
  35.      */ 
  36.     protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) { 
  37.         try { 
  38.             PropertyValues propertyValues = beanDefinition.getPropertyValues(); 
  39.             for (PropertyValue propertyValue : propertyValues.getPropertyValues()) { 
  40.  
  41.                 String name = propertyValue.getName(); 
  42.                 Object value = propertyValue.getValue(); 
  43.  
  44.                 if (value instanceof BeanReference) { 
  45.                     // A 依賴 B,獲取 B 的實(shí)例化 
  46.                     BeanReference beanReference = (BeanReference) value; 
  47.                     value = getBean(beanReference.getBeanName()); 
  48.                 } 
  49.                 // 屬性填充 
  50.                 BeanUtil.setFieldValue(bean, name, value); 
  51.             } 
  52.         } catch (Exception e) { 
  53.             throw new BeansException("Error setting property values:" + beanName); 
  54.         } 
  55.     } 
  56.  
  57.     public InstantiationStrategy getInstantiationStrategy() { 
  58.         return instantiationStrategy; 
  59.     } 
  60.  
  61.     public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) { 
  62.         this.instantiationStrategy = instantiationStrategy; 
  63.     } 
  64.  

這個(gè)類的內(nèi)容稍微有點(diǎn)長,主要包括三個(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)

五、測試

1. 事先準(zhǔn)備

cn.bugstack.springframework.test.bean.UserDao

  1. public class UserDao { 
  2.  
  3.     private static Map<String, String> hashMap = new HashMap<>(); 
  4.  
  5.     static { 
  6.         hashMap.put("10001""小傅哥"); 
  7.         hashMap.put("10002""八杯水"); 
  8.         hashMap.put("10003""阿毛"); 
  9.     } 
  10.  
  11.     public String queryUserName(String uId) { 
  12.         return hashMap.get(uId); 
  13.     } 
  14.  

cn.bugstack.springframework.test.bean.UserService

  1. public class UserService { 
  2.  
  3.     private String uId; 
  4.  
  5.     private UserDao userDao; 
  6.  
  7.     public void queryUserInfo() { 
  8.         System.out.println("查詢用戶信息:" + userDao.queryUserName(uId)); 
  9.     } 
  10.  
  11.     // ...get/set 

Dao、Service,是我們平常開發(fā)經(jīng)常使用的場景。在 UserService 中注入 UserDao,這樣就能體現(xiàn)出Bean屬性的依賴了。

2. 測試用例

  1. @Test 
  2. public void test_BeanFactory() { 
  3.     // 1.初始化 BeanFactory 
  4.     DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();   
  5.  
  6.     // 2. UserDao 注冊 
  7.     beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));    
  8.  
  9.     // 3. UserService 設(shè)置屬性[uId、userDao] 
  10.     PropertyValues propertyValues = new PropertyValues(); 
  11.     propertyValues.addPropertyValue(new PropertyValue("uId""10001")); 
  12.     propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao")));   
  13.  
  14.     // 4. UserService 注入bean 
  15.     BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues); 
  16.     beanFactory.registerBeanDefinition("userService", beanDefinition);     
  17.  
  18.     // 5. UserService 獲取bean 
  19.     UserService userService = (UserService) beanFactory.getBean("userService"); 
  20.     userService.queryUserInfo(); 

與直接獲取 Bean 對(duì)象不同,這次我們還需要先把 userDao 注入到 Bean 容器中。beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));

接下來就是屬性填充的操作了,一種是普通屬性 new PropertyValue("uId", "10001"),另外一種是對(duì)象屬性 new PropertyValue("userDao",new BeanReference("userDao"))

接下來的操作就簡單了,只不過是正常獲取 userService 對(duì)象,調(diào)用方法即可。

3. 測試結(jié)果

查詢用戶信息:小傅哥

Process finished with exit code 0

  • 從測試結(jié)果看我們的屬性填充已經(jīng)起作用了,因?yàn)橹挥袑傩蕴畛浜?,才能調(diào)用到Dao方法,如:userDao.queryUserName(uId)
  • 那么我們在看看Debug調(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ì)要比僅僅是做簡單實(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ì)簡化一些流程,但你可以拿相同的類名,去搜到每一個(gè)功能在 Spring 源碼中的實(shí)現(xiàn)。

 

責(zé)任編輯:武曉燕 來源: bugstack蟲洞棧
相關(guān)推薦

2023-06-27 08:58:13

quarkusBean

2023-07-11 09:14:12

Beanquarkus

2011-05-04 13:58:56

奔圖激光打印機(jī)

2023-06-29 08:32:41

Bean作用域

2019-06-11 18:35:18

戴爾

2011-05-26 13:46:09

2015-12-22 17:48:59

2024-08-13 12:49:29

2023-10-07 08:35:07

依賴注入Spring

2021-08-04 11:39:17

Bean對(duì)象配置

2023-03-08 09:59:39

SpringBean注入

2023-01-30 22:10:12

BeanSpring容器

2021-05-06 07:58:57

Spring BeanIOCAOP

2021-07-01 10:45:18

Bean對(duì)象作用域

2024-03-18 00:00:00

SpringBean設(shè)計(jì)

2021-03-08 08:40:25

Spring Bean 創(chuàng)建單例對(duì)象

2022-03-16 11:11:37

SpringBean項(xiàng)目

2011-04-02 15:25:41

Spring

2023-03-27 21:54:48

方式OptionalAutowird

2011-06-15 12:36:58

JSPBEAN
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)