org.springframework.core.styler包解读

简介:
这个包的说明是: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类图:
style.jpg
    首先,你需要理解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));
    }
   dot.gifdot.gif一系列不同类型的append方法
   
public  String toString() {
// 结束,并返回优美格式的String        this.styler.styleEnd(this.buffer, this.object);
         return   this .buffer.toString();
    }
文章转自庄周梦蝶  ,原文发布时间5.17
目录
相关文章
|
6月前
|
Java Serverless Spring
org.springframework.boot.loader.JarLaunche
org.springframework.boot.loader.JarLaunche
265 3
|
Java Maven
程序包org.springframework.transaction.annotation不存在
整合ssm报:程序包org.springframework.transaction.annotation不存在 使用注解: @Transactional 之后,就一直报不存在 最终找到原因是:maven依赖 spring-tx版本问题,换个版本就OK
1413 0
程序包org.springframework.transaction.annotation不存在
|
11月前
|
Java Spring
IDEA中使用org.springframework.boot.autoconfigure.AutoConfiguration.imports没有被识别
IDEA中使用org.springframework.boot.autoconfigure.AutoConfiguration.imports没有被识别
459 0
|
8月前
|
Java
解决出现的java: 无法访问org.springframework.boot.SpringApplication问题~
解决出现的java: 无法访问org.springframework.boot.SpringApplication问题~
327 0
|
Java
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
1447 0
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
|
7月前
解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchExceptio
解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchExceptio
289 0
|
IDE Java Maven
Error:(5, 45) java: 程序包org.springframework.boot.test.context不存在 (超级好用,不好用,你找我)
Error:(5, 45) java: 程序包org.springframework.boot.test.context不存在 (超级好用,不好用,你找我)
612 0
Error:(5, 45) java: 程序包org.springframework.boot.test.context不存在 (超级好用,不好用,你找我)
Error:(3, 29) java: 程序包org.junit.jupiter.api不存在
Error:(3, 29) java: 程序包org.junit.jupiter.api不存在
Error:(3, 29) java: 程序包org.junit.jupiter.api不存在
|
Java 应用服务中间件 Android开发
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
导入所需依赖并引用如下核心标签库后报错,无法使用JSTL的原因及解决方案
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
|
网络安全
SSH框架整合遇到的错误——org.springframework.beans.NotWritablePropertyException:
提示错误信息: Unable to instantiate Action, userAction, defined for 'user_registPage' in namespace '/'Error creating bean with name 'userAction' defined in class path resource [applicationContext.
1612 0

热门文章

最新文章