最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring之關(guān)于PropertyDescriptor的擴展剖析

 更新時間:2023年07月07日 08:57:27   作者:StrangerIt  
這篇文章主要介紹了Spring之關(guān)于PropertyDescriptor的擴展剖析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、jdk中類PropertyDescriptor獲取

jdk中Introspector類為工具提供了一種標(biāo)準(zhǔn)的方法來了解目標(biāo)Java Bean支持的屬性、事件和方法。

 java.beans.Introspector#getTargetPropertyInfo
	private PropertyDescriptor[] getTargetPropertyInfo() {
        // Apply some reflection to the current class.
        // First get an array of all the public methods at this level
        Method methodList[] = getPublicDeclaredMethods(beanClass);
        // Now analyze each method.
        //遍歷所有方法看是否是符合PropertyDescriptor
        for (int i = 0; i < methodList.length; i++) {
            Method method = methodList[i];
            if (method == null) {
                continue;
            }
            // skip static methods.
            int mods = method.getModifiers();
            if (Modifier.isStatic(mods)) {
                continue;
            }
            String name = method.getName();
            Class<?>[] argTypes = method.getParameterTypes();
            Class<?> resultType = method.getReturnType();
            int argCount = argTypes.length;
            PropertyDescriptor pd = null;
            if (name.length() <= 3 && !name.startsWith(IS_PREFIX)) {
                // Optimization. Don't bother with invalid propertyNames.
                continue;
            }
            try {
                if (argCount == 0) {
                    /**
                     * 方法沒有參數(shù):方法有readMethod沒有writeMehtod
                     *    1、普通get開頭方法
                     *    2、返回值boolean 以is開頭的
                     */
                    if (name.startsWith(GET_PREFIX)) {
                        // Simple getter
                        pd = new PropertyDescriptor(this.beanClass, name.substring(3), method, null);
                    } else if (resultType == boolean.class && name.startsWith(IS_PREFIX)) {
                        // Boolean getter
                        pd = new PropertyDescriptor(this.beanClass, name.substring(2), method, null);
                    }
                } else if (argCount == 1) {
                    /**
                     * 有一個參數(shù)
                     * 1、有一個參數(shù)且int類型,方法get開頭的,沒有readMethod  writeMehtod等屬性
                     * 2、沒有返回值、set方法開頭的,具有writeMethod
                     */
                    if (int.class.equals(argTypes[0]) && name.startsWith(GET_PREFIX)) {
                        pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, method, null);
                    } else if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) {
                        // Simple setter
                        pd = new PropertyDescriptor(this.beanClass, name.substring(3), null, method);
                        if (throwsException(method, PropertyVetoException.class)) {
                            pd.setConstrained(true);
                        }
                    }
                } else if (argCount == 2) {
                    /**
                     * 兩個參數(shù)
                     * 1、返回值void ,第一個參數(shù)int類型,set開頭的會生成PropertyDescriptor(注意此時沒有writeMethod)
                     */
                    if (void.class.equals(resultType) && int.class.equals(argTypes[0]) && name.startsWith(SET_PREFIX)) {
                        pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, null, method);
                        if (throwsException(method, PropertyVetoException.class)) {
                            pd.setConstrained(true);
                        }
                    }
                }
            } catch (IntrospectionException ex) {
                pd = null;
            }
            if (pd != null) {
                if (propertyChangeSource) {
                    pd.setBound(true);
                }
                addPropertyDescriptor(pd);
            }
        }
    processPropertyDescriptors();
}

總結(jié)滿足以下條件才會生成PropertyDescriptor(注意讀寫方法是否為空,spring中by_type類型注入會篩選出具有寫方法不為空的PropertyDescriptor):

1、參數(shù)個數(shù)必須2個以內(nèi)、方法不是static

2、 方法沒有參數(shù):方法有readMethod沒有writeMehtod

  • 普通get開頭方法
  • 返回值boolean 以is開頭的

3、 有一個參數(shù)

  • 有一個參數(shù)且int類型,方法get開頭的,沒有readMethod writeMehtod等屬性
  • 沒有返回值、set方法開頭的,具有writeMethod

4、兩個參數(shù)

  • 返回值void ,第一個參數(shù)int類型,set開頭的會生成PropertyDescriptor(注意此時沒有writeMethod)

綜上所述:具有寫方法的必須返回值void 且set開頭一個參數(shù)的的才有寫方法(spring中by_type類型注入會篩選出具有寫方法不為空的)

demo 具有寫方法的PropertyDescriptor演示:

//@Component
public class UserService {
    private  OrderService  orderService;
   //返回值不為void
    public  OrderService  setOrderService(OrderService orderService){
        //this.orderService=orderService;
        return orderService;
    }
    //返回值不為void
    public  OrderService  setOrderService(int  test,OrderService orderService){
        this.orderService=orderService;
        return orderService;
    }
    //返回值void 一個參數(shù)滿足要求
    public  void  setService12123(OrderService orderService1){
       System.out.println("1231"+orderService);
    }
    //返回值void 參數(shù)個數(shù)大于2不滿足
    public  void  setOrderService(int  test,OrderService orderService,StockService stockService){
        this.orderService=orderService;
    }
}
public class Test {
    public static void main(String[] args) throws IntrospectionException {
       BeanInfo beanInfo= Introspector.getBeanInfo(UserService.class);
       PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
       for(PropertyDescriptor propertyDescriptor:propertyDescriptors){
           System.out.println(propertyDescriptor.getWriteMethod());
       }
    }
}

此時滿足條件方法有g(shù)etClass(繼承父類的Object) 、setService12123會生成PropertyDescriptor且具有寫方法

存在問題

方法有返回值、且靜態(tài)的方法是不具備生成PropertyDescriptor屬性描述器,spring中

org.springframework.beans.ExtendedBeanInfo#isCandidateWriteMethod

拓展有返回值、或者static也會生成PropertyDescriptor**

滿足以下條件:

return methodName.length() > 3 && methodName.startsWith(“set”) && Modifier.isPublic(method.getModifiers()) && (!Void.TYPE.isAssignableFrom(method.getReturnType()) || Modifier.isStatic(method.getModifiers())) && (nParams == 1 || nParams == 2 && Integer.TYPE == method.getParameterTypes()[0]);

二、spring針對jdk進行拓展

ExtendedBeanInfo對jdk不滿足的方法進行擴展(有返回值、static)生成PropertyDescriptor

**滿足以下條件才會生成PropertyDescriptor
? ?1、set開頭方法
? ?2、public方法
? ?3、返回值不是void或者是靜態(tài)
? ?4、參數(shù)一個或者2個(2個實話第一個參數(shù)必須為int類型)**
//1、set開頭方法
       2、public方法
       3、返回值不是void或者是靜態(tài)
       4、參數(shù)一個或者2個(2個實話第一個參數(shù)必須為int類型)
    public static boolean isCandidateWriteMethod(Method method) {
        String methodName = method.getName();
        int nParams = method.getParameterCount();
        return methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) && (!Void.TYPE.isAssignableFrom(method.getReturnType()) || Modifier.isStatic(method.getModifiers())) && (nParams == 1 || nParams == 2 && Integer.TYPE == method.getParameterTypes()[0]);
    }

三、總結(jié)

spring依賴注入(@Bean(autowire=Autowire.BY_TYPE)實話會找到該類所有PropertyDescriptor滿足條件的方法

  • 1、從jdk中Introspector中獲取
  • 2、擴展ExtendedBeanInfo獲取

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Spring Data JPA系列之投影(Projection)的用法

    詳解Spring Data JPA系列之投影(Projection)的用法

    本篇文章主要介紹了詳解Spring Data JPA系列之投影(Projection)的用法,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • java實現(xiàn)消息隊列的兩種方式(小結(jié))

    java實現(xiàn)消息隊列的兩種方式(小結(jié))

    本文主要介紹了兩種java實現(xiàn)消息隊列的方式,利用Spring消息模板發(fā)送消息和Apache ActiveMQ官方實例發(fā)送消息,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain

    解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain

    這篇文章主要介紹了解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java實現(xiàn)無頭雙向鏈表操作

    Java實現(xiàn)無頭雙向鏈表操作

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)無頭雙向鏈表的基本操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 如何使用新方式編寫Spring MVC接口

    如何使用新方式編寫Spring MVC接口

    這篇文章主要介紹了如何使用新方式編寫Spring MVC接口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot如何統(tǒng)一配置bean的別名

    SpringBoot如何統(tǒng)一配置bean的別名

    這篇文章主要介紹了SpringBoot如何統(tǒng)一配置bean的別名,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 簡單了解SpringMVC緩存對靜態(tài)資源有什么影響

    簡單了解SpringMVC緩存對靜態(tài)資源有什么影響

    這篇文章主要介紹了簡單了解SpringMVC緩存對靜態(tài)資源有什么影響,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot中內(nèi)置的49個常用工具類使用

    SpringBoot中內(nèi)置的49個常用工具類使用

    SpringBoot以其強大的自動配置和豐富的生態(tài)系統(tǒng)成為Java開發(fā)的首選框架,本文將介紹49個常用工具類,并通過簡潔的代碼示例展示它們的基本用法,希望對大家有一定的幫助
    2025-04-04
  • Java通過ServerSocket與Socket實現(xiàn)通信過程

    Java通過ServerSocket與Socket實現(xiàn)通信過程

    本文介紹了Java中的ServerSocket和Socket類,詳細(xì)講解了它們的構(gòu)造方法和使用場景,并通過一個簡單的通信示例展示了如何實現(xiàn)服務(wù)器和客戶端之間的通信,此外,還介紹了如何為Socket設(shè)置超時時間
    2025-11-11
  • Java使用RedisTemplate操作Redis遇到的坑

    Java使用RedisTemplate操作Redis遇到的坑

    這篇文章主要介紹了Java使用RedisTemplate操作Redis遇到的坑,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評論

宁波市| 碌曲县| 松潘县| 温泉县| 郑州市| 宜宾市| 克什克腾旗| 通州市| 德州市| 白河县| 深州市| 吉水县| 赤水市| 嘉义市| 青铜峡市| 瑞安市| 淄博市| 隆昌县| 灵丘县| 繁昌县| 武宁县| 济阳县| 湖南省| 吉安县| 九台市| 安达市| 陵川县| 邯郸市| 新晃| 剑阁县| 敦化市| 兴城市| 双辽市| 攀枝花市| 安顺市| 清徐县| 荃湾区| 贞丰县| 额济纳旗| 汉源县| 阿克|