溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Bean實例過程中使用反射和遞歸處理的Bean屬性填充問題

發(fā)布時間:2021-06-04 10:43:56 來源:億速云 閱讀:162 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Spring Bean實例過程中使用反射和遞歸處理的Bean屬性填充問題,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

一、前言

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

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

就編程開發(fā)來講,丟三落四、亂碼七糟,可能這就是大部分初級程序員日常開發(fā)的真實寫照,在即使有測試人員驗證的情況下,也會出現(xiàn)帶Bug上線的現(xiàn)象,只不過是當(dāng)時沒有發(fā)現(xiàn)而已!因為是人寫代碼,就一定會有錯誤,即使是老碼農(nóng)

就程序Bug來講,會包括產(chǎn)品PRD流程上的Bug、運營配置活動時候的Bug、研發(fā)開發(fā)時功能實現(xiàn)的Bug、測試驗證時漏掉流程的Bug、上線過程中運維服務(wù)相關(guān)配置的Bug,而這些其實都可以通過制定的流程規(guī)范和一定的研發(fā)經(jīng)驗積累,慢慢盡可能減少。

而另外一類是溝通留下的Bug,通常情況下業(yè)務(wù)提需求、產(chǎn)品定方案、研發(fā)做實現(xiàn),最終還要有UI、測試、運營、架構(gòu)等等各個環(huán)節(jié)的人員參與到一個項目的承接、開發(fā)到上線運行,而在這一群人需要保持一個統(tǒng)一的信息傳播其實是很難的。比如在項目開發(fā)中期,運營給產(chǎn)品說了一個新增的需求,產(chǎn)品覺得功能也不大,隨即找到對應(yīng)的前端研發(fā)加個邏輯,但沒想到可能也影響到了后端的開發(fā)和測試的用例。最后功能雖然是上線了,可并不在整個產(chǎn)研測的需求覆蓋度范圍里,也就隱形的埋下了一個坑。

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

二、目標(biāo)

首先我們回顧下這幾章節(jié)都完成了什么,包括:實現(xiàn)一個容器、定義和注冊Bean、實例化Bean,按照是否包含構(gòu)造函數(shù)實現(xiàn)不同的實例化策略,那么在創(chuàng)建對象實例化這我們還缺少什么?其實還缺少一個關(guān)于類中是否有屬性的問題,如果有類中包含屬性那么在實例化的時候就需要把屬性信息填充上,這樣才是一個完整的對象創(chuàng)建。

對于屬性的填充不只是 int、Long、String,還包括還沒有實例化的對象屬性,都需要在 Bean 創(chuàng)建時進(jìn)行填充操作。不過這里我們暫時不會考慮 Bean 的循環(huán)依賴,否則會把整個功能實現(xiàn)撐大,這樣新人學(xué)習(xí)時就把握不住了,待后續(xù)陸續(xù)先把核心功能實現(xiàn)后,再逐步完善

三、設(shè)計

鑒于屬性填充是在 Bean 使用 newInstance 或者 Cglib 創(chuàng)建后,開始補全屬性信息,那么就可以在類 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加補全屬性方法。這部分大家在實習(xí)的過程中也可以對照Spring源碼學(xué)習(xí),這里的實現(xiàn)也是Spring的簡化版,后續(xù)對照學(xué)習(xí)會更加易于理解

Spring Bean實例過程中使用反射和遞歸處理的Bean屬性填充問題

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

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

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

四、實現(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

Spring Bean實例過程中使用反射和遞歸處理的Bean屬性填充問題

  • 本章節(jié)中需要新增加3個類,BeanReference(類引用)、PropertyValue(屬性值)、PropertyValues(屬性集合),分別用于類和其他類型屬性填充操作。

  • 另外改動的類主要是 AbstractAutowireCapableBeanFactory,在 createBean 中補全屬性填充部分。

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;
    }

}

這兩個類的作用就是創(chuàng)建出一個用于傳遞類中屬性信息的類,因為屬性可能會有很多,所以還需要定義一個集合包裝下。

3. Bean定義補全

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 注冊的過程中是需要傳遞 Bean 的信息,在幾個前面章節(jié)的測試中都有所體現(xiàn) new BeanDefinition(UserService.class, propertyValues);

  • 所以為了把屬性一定交給 Bean 定義,所以這里填充了 PropertyValues 屬性,同時把兩個構(gòu)造函數(shù)做了一些簡單的優(yōu)化,避免后面 for 循環(huán)時還得判斷屬性填充是否為空。

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 的實例化
                    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;
    }

}
  • 這個類的內(nèi)容稍微有點長,主要包括三個方法:createBean、createBeanInstance、applyPropertyValues,這里我們主要關(guān)注 createBean 的方法中調(diào)用的 applyPropertyValues 方法。

  • 在 applyPropertyValues 中,通過獲取 beanDefinition.getPropertyValues() 循環(huán)進(jìn)行屬性填充操作,如果遇到的是 BeanReference,那么就需要遞歸獲取 Bean 實例,調(diào)用 getBean 方法。

  • 當(dāng)把依賴的 Bean 對象創(chuàng)建完成后,會遞歸回現(xiàn)在屬性填充中。這里需要注意我們并沒有去處理循環(huán)依賴的問題,這部分內(nèi)容較大,后續(xù)補充。BeanUtil.setFieldValue(bean, name, value) 是 hutool-all 工具類中的方法,你也可以自己實現(xiàn)

五、測試

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)常使用的場景。在 UserService 中注入 UserDao,這樣就能體現(xiàn)出Bean屬性的依賴了。

2. 測試用例

@Test
public void test_BeanFactory() {
    // 1.初始化 BeanFactory
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();  

    // 2. UserDao 注冊
    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 對象不同,這次我們還需要先把 userDao 注入到 Bean 容器中。beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));

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

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

3. 測試結(jié)果

查詢用戶信息:小傅哥

Process finished with exit code 0
  • 從測試結(jié)果看我們的屬性填充已經(jīng)起作用了,因為只有屬性填充后,才能調(diào)用到Dao方法,如:userDao.queryUserName(uId)

  • 那么我們在看看Debug調(diào)試的情況下,有沒有進(jìn)入到實現(xiàn)的 Bean 屬性填充中,如下:

Spring Bean實例過程中使用反射和遞歸處理的Bean屬性填充問題

好,就是截圖這里,我們看到已經(jīng)開始進(jìn)行屬性填充操作了,當(dāng)發(fā)現(xiàn)屬性是 BeanReference 時,則需要獲取創(chuàng)建 Bean 實例。

六、總結(jié)

  • 在本章節(jié)中我們把 AbstractAutowireCapableBeanFactory 類中的創(chuàng)建對象功能又做了擴充,依賴于是否有構(gòu)造函數(shù)的實例化策略完成后,開始補充 Bean 屬性信息。當(dāng)遇到 Bean 屬性為 Bean 對象時,需要遞歸處理。最后在屬性填充時需要用到反射操作,也可以使用一些工具類處理。

  • 每一個章節(jié)的功能點我們都在循序漸進(jìn)的實現(xiàn),這樣可以讓新人更好的接受關(guān)于 Spring 中的設(shè)計思路。尤其是在一些已經(jīng)開發(fā)好的類上,怎么擴充新的功能時候的設(shè)計更為重要。學(xué)習(xí)編程有的時候?qū)W習(xí)思路設(shè)計要比僅僅是做簡單實現(xiàn),更能提升編程思維。

  • 到這一章節(jié)關(guān)于 Bean 的創(chuàng)建操作就開發(fā)完成了,接下來需要整個框架的基礎(chǔ)上完成資源屬性的加載,就是我們需要去動 Xml 配置了,讓我們這小框架越來越像 Spring。另外在框架實現(xiàn)的過程中所有的類名都會參考 Spring 源碼,以及相應(yīng)的設(shè)計實現(xiàn)步驟也是與 Spring 源碼中對應(yīng),只不過會簡化一些流程,但你可以拿相同的類名,去搜到每一個功能在 Spring 源碼中的實現(xiàn)。

關(guān)于“Spring Bean實例過程中使用反射和遞歸處理的Bean屬性填充問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI