java propertydescriptor_Spring Integration

java propertydescriptor_Spring Integration总结满足以下条件才会生成PropertyDescriptor(注意读写方法是否为空,spring中by_type类型注入会筛选出具有写方法不为空的PropertyDescriptor):1、参数个数必须2个以内、方法不是static2、方法没有参数:方法有readMethod没有writeMehtod1、普通get开头方法2、返回值boolean以is开头的3、有一个参数1、有一个参数且int类型,方法get开头的,没有readMethodwriteMehtod等属性2、没有返回值、

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

一、jdk中类PropertyDescriptor获取

  jdk中Introspector类为工具提供了一种标准的方法来了解目标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) {
                    /**
                     * 方法没有参数:方法有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) {
                    /**
                     * 有一个参数
                     * 1、有一个参数且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) {
                    /**
                     * 两个参数
                     * 1、返回值void ,第一个参数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();


}

总结满足以下条件才会生成PropertyDescriptor(注意读写方法是否为空,spring中by_type类型注入会筛选出具有写方法不为空的PropertyDescriptor):
1、参数个数必须2个以内、方法不是static
2、 方法没有参数:方法有readMethod没有writeMehtod
1、普通get开头方法
2、返回值boolean 以is开头的
3、 有一个参数
1、有一个参数且int类型,方法get开头的,没有readMethod writeMehtod等属性
2、没有返回值、set方法开头的,具有writeMethod
4、两个参数
1、返回值void ,第一个参数int类型,set开头的会生成PropertyDescriptor(注意此时没有writeMethod)

综上所述:具有写方法的必须返回值void 且set开头一个参数的的才有写方法(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 一个参数满足要求
    public  void  setService12123(OrderService orderService1){ 
   
       System.out.println("1231"+orderService);
    }
    //返回值void 参数个数大于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());
       }
    }
}
此时满足条件方法有getClass(继承父类的Object) 、setService12123会生成PropertyDescriptor且具有写方法

在这里插入图片描述

存在问题:
方法有返回值、且静态的方法是不具备生成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或者是静态
   4、参数一个或者2个(2个实话第一个参数必须为int类型)**
     //1、set开头方法
       2public方法
       3、返回值不是void或者是静态
       4、参数一个或者2个(2个实话第一个参数必须为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]);
    }

三、总结

spring依赖注入(@Bean(autowire=Autowire.BY_TYPE)实话会找到该类所有PropertyDescriptor满足条件的方法
1、从jdk中Introspector中获取
2、扩展ExtendedBeanInfo获取

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188727.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Postman 汉化(Postman中文版)

    Postman 汉化(Postman中文版)postman官网下载地址https://www.postman.com/downloads/postman汉化包https://github.com/hlmd/Postman-cn/releases1.首先从官网下载postMan安装包2.下载postMan汉化包(app.zip)3.将汉化包解压并复制到Postman目录下4.重启postMan即可完成汉化…

    2022年10月24日
    0
  • android之相机开发

    在android中应用相机功能,一般有两种:一种是直接调用系统相机,一种自己写的相机。我将分别演示两种方式的使用:第一种:是使用Intent跳转到系统相机,action为:android.media.action.STILL_IMAGE_CAMERA关键代码:Intent intent = new Intent(); //调用照相机 intent.setAction(

    2022年3月10日
    41
  • CRTMP视频直播服务器部署及测试

    CRTMP视频直播服务器部署及测试一、搭建CRTMP视频直播服务器1、下载CRTMP服务器软件svnco–usernameanonymous–password””https://svn.rtmpd.com/crtmpserver/trunkcrtmpserver2、进入一下目录,

    2022年6月9日
    44
  • Redis(Redis简介和基本命令)[通俗易懂]

    Redis(Redis简介和基本命令)[通俗易懂]这里写目录标题NoSQLRedisNoSQLNoSQL == Not Only SQL(不仅仅是关系型数据库)出现原因:随着web2.0互联网的诞生,传统的关系型数据库很难对付web2.0时代!尤其是超大规模的高并发的社区,暴露出来很多难以克服的问题,NoSQL在当今大数据环境下发展的十分迅速,Redis是发展最快的。RDBMS 和 NoSQL的对比RDBMS 结构化组织SQL数据和关系都存在单独的表中 row col操作,数据定义语言严格的一致性基础的事务NoSQL不仅仅是数

    2022年8月8日
    3
  • pytest fixtures_pytest命令

    pytest fixtures_pytest命令fixture的优势Pytest的fixture相对于传统的xUnit的setup/teardown函数做了显著的改进:命名方式灵活,不局限于setup和teardown这几个命名conf

    2022年7月31日
    0
  • Android开发:什么是IBinder

    Android开发:什么是IBinder上回书简单描述了进程间传递类对象的原理,这回在讲Parcel之前,先要讲一个东西:IBinder。IBinder是什么呢?首先要明白,Android的远程调用(就是跨进程调用)就是通过IBinder实现的,下面是对android开发文档的翻译。IBinder是远程对象的基本接口,是为高性能而设计的轻量级远程调用机制的核心部分。但它不仅用于

    2022年4月9日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号