这个包的说明是:Support for styling values as Strings, with ToStringCreator as central class.
这个包简单来说就是提供一个pretty-printing功能的辅助类,而ToStringCreator就是用于产生一个可以输出经过美化的value信息的toString()方法。使用方法参照spring的Test可以看到是这样:

        int[] integers = new int[] { 0, 1, 2, 3, 4 };
        String str = new ToStringCreator(integers).toString();
        assertEquals("[@" + ObjectUtils.getIdentityHexString(integers) + " array<Integer>[0, 1, 2, 3, 4]]", str);

或者写个简单例子感受下:

int [] a={1,2,3,4,5,6,7,8,9};
System.out.println(new ToStringCreator(a).toString());

输出:

[@18558d2 array<Integer>[1, 2, 3, 4, 5, 6, 7, 8, 9]]

如果你接触过ruby,你应该很熟悉Object.inpsect这个功能,这里通过ToStringCreator包装的toString()方法也是产生类似的能够清晰显示对象内部结构信息的方法。spring应该是使用这些辅助类来报告清晰的错误信息或者提示信息。
    看看这个包的UML类图:

    首先,你需要理解ToStringStyler和ValueStyle两个接口,ToStringStyler定义了描述一个输入的Value信息的基本模板方法:

public interface ToStringStyler {

/**
     * Style a <code>toString()</code>'ed object before its fields are styled.
     * @param buffer the buffer to print to
     * @param obj the object to style
     */
    void styleStart(StringBuffer buffer, Object obj);

/**
     * Style a <code>toString()</code>'ed object after it's fields are styled.
     * @param buffer the buffer to print to
     * @param obj the object to style
     */
    void styleEnd(StringBuffer buffer, Object obj);

/**
     * Style a field value as a string.
     * @param buffer the buffer to print to
     * @param fieldName the he name of the field
     * @param value the field value
     */
    void styleField(StringBuffer buffer, String fieldName, Object value);

/**
     * Style the given value.
     * @param buffer the buffer to print to
     * @param value the field value
     */
    void styleValue(StringBuffer buffer, Object value);

/**
     * Style the field separator.
     * @param buffer buffer to print to
     */
    void styleFieldSeparator(StringBuffer buffer);

}

这是典型的Template Method模式,而两个接口ToStringStyler、ValueStyler和它们的相应实现DefaultToStringStyler、DefaultValueStyler又是策略模式(Strategy)的应用体现。ValueStyler和DefaultValueStyler之间不仅仅是策略模式,同时也是visitor模式,请看DefaultValueStyler中一系列重载的visit方法,这些visit方法访问不同类型Value的内部结构并构造pretty格式的String返回,提供给ToStringStyler使用。
    ToStringCreator是ToStringStyler的客户,它使用ToStringStyler调用产生优美格式打印,而ToStringStyler 其实又是使用ValueStyler是访问每个不同类型的子元素并返回优美格式的String。实现的相当精巧和灵活:

public ToStringCreator(Object obj, ToStringStyler styler) {
        Assert.notNull(obj, "The object to be styled is required");
        this.object = obj;
        this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER);
//开始        this.styler.styleStart(this.buffer, this.object);
    }

public ToStringCreator append(String fieldName, byte value) {
        return append(fieldName, new Byte(value));
    }
   一系列不同类型的append方法
   public String toString() {
//结束,并返回优美格式的String        this.styler.styleEnd(this.buffer, this.object);
        return this.buffer.toString();
    }

文章转自庄周梦蝶  ,原文发布时间5.17

org.springframework.core.styler包解读相关推荐

  1. spring源码分析-core.io包里面的类

    前些日子看<深入理解javaweb开发>时,看到第一章java的io流,发觉自己对io流真的不是很熟悉.然后看了下JDK1.7中io包的一点点代码,又看了org.springframewo ...

  2. Spring Mybatis 异常报错:org.springframework.core.annotation.AnnotationUtils.clearCache()V

    今天在学习Spring 的时候,运行测试出现了异常: "C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" -javaagent:D: ...

  3. org.springframework.core.type.AnnotationMetadata.introspect(Ljava/lang/Class;)Lorg/springframework/c

    使用tomacat发布的时候一开始还没有这个问题.不知道什么时候就突然出现这个问题了. 全部的描述是这样的: Description:An attempt was made to call a met ...

  4. ssh整合常见异常 springframework.core.CollectionFactory.createConcurrentMapIfPossible(I)Ljava/util/Map;

    简短的异常信息如下: java.lang.NoSuchMethodError: org.springframework.core.CollectionFactory.createConcurrentM ...

  5. NoClassDefFoundError: org/springframework/core/ErrorCoded 报错

    Spring项目中引入: <dependency><groupId>org.springframework.data</groupId><artifactId ...

  6. org/springframework/core/MethodClassKey

    解决下面报错的办法: 把pom.xml中下面两个dependency的version去掉. <dependency><groupId>org.springframework&l ...

  7. 问题 : lang.NoClassDefFoundError: org/springframework/core/annotation/AnnotatedElementUtils,的解决方法

    问题 : lang.NoClassDefFoundError: org/springframework/core/annotation/AnnotatedElementUtils,的解决方法 参考文章 ...

  8. Cannot serialize; nested exception is org.springframework.core.serializer

    问题描述: Cannot serialize; nested exception is org.springframework.core.serializer.support.Serializatio ...

  9. 关于java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup问题的解决方法

    在初学spring整合jdbc,mybatis,test时,通过注解开发方式获取spring容器时执行到如下语句时报错: 错误详细为: Exception in thread "main&q ...

  10. org.springframework.core.metrics.ApplicationStartup

    在进行使用SOP框架调试微服务时, 使用springboot 写了一个服务的例子: sop版本信息: <dependency><groupId>com.gitee.sop< ...

最新文章

  1. inux下如何查看CPU信息, 包括位数和多核信息
  2. 【STM32】F1 系列驱动全彩显示屏
  3. DB_NAME、DB_UNIQUE_NAME、SERVICE_NAME和INSTANCE_NAME等的区别
  4. 由“深”至“广”,探索2022音视频技术的无限可能
  5. SAP 电商云 Spartacus UI 的 proxy facade 是如何调用实际实现类
  6. ansys matlab 调用,matlab 调用ansys (转载)
  7. Android平台RTMP推流或轻量级RTSP服务(摄像头或同屏)编码前数据接入类型总结
  8. 友元是c还是java_[C++]【类】友元的三种声明及注意事项
  9. HttpURLConnection中connection.getInputStream()报异常FileNotFoundException
  10. Java中JTextField.getText() 和 JPassword.getPassword() 的区别
  11. 华为高管揭秘公司不上市的原因;微信回应「花钱就能查到聊天记录」;马斯克删除「超苹果只要几个月」评论 | 极客头条...
  12. 简单的高可用集群实验
  13. 三菱FX 系列PLC 编程通信协议总览
  14. spring源码:循环依赖源码学习
  15. 《数据库系统基础教程》概念
  16. ASP.NET动态网站开发培训-26.在线编辑器FreeTextBox的使用
  17. 如何获取联盟后台的s.click.taobao.com的短链接
  18. 华为携西班牙电信于智利设立NB-IoT开放实验室
  19. 美团后端一二面c++
  20. JEECG集成flowable

热门文章

  1. 华中农业计算机硕士就业,华中农业大学好就业吗?附华中农业大学就业率最高的专业名单...
  2. Java基础--继承
  3. 微信公众号--生成带参数的二维码 {“errcode“:48001,“errmsg“:“api unauthorized rid: 60520af9-71ff2283-63d36e0d“}
  4. 基于QT5、C/S(客户端/服务器)架构的MiniQQ项目开发
  5. T3677 道生一——dfs
  6. mysql5.6 0000-00-00 00:00:00_Mysql sql_mode设置 timestamp default 0000-00-00 00:00:00 创建表失败处理...
  7. 【数据科学赛】CAIL 2022 #八赛道 #NLP #文本匹配 #信息抽取
  8. 【历史上的今天】4 月 3 日:亚马逊卖出第一本书;世界上第一通手机电话;IBM 计算机先驱出生
  9. 网页游戏的项目设计方案分享
  10. 智能仓储系统作业流程及价值