Detailed description of Java output JavaBean attributes using the common package ~~~ Important

Source: Internet
Author: User

Today, I started to learn about Jakarta commons Lang. I am reading o'reilly's Jakarta commons cookbook.

The reflectiontostringbuilder class is used to implement the tostring () method in the class. It adopts the Java reflection mechanism (reflection) and uses the accessibleobject class in the reflection package to bypass access control and directly access the Private Members of the object. Therefore, when using this class, pay attention to the security policy of the runtime environment.



### Print attributes of JavaBean:1. Application Scenario: when calling WS, you often need to check whether the parameters in the call (request) parameter query are set correctly. In this case, you want to print the attributes and their values. 2. Method: override the tostring function in this request class and add the reflectiontostring. tostring (this) function of the common package to the function! Public String tostring () {return reflectiontostringbuilder. tostring (this, tostringstyle. multi_line_style);} print: system. Out. println (CQC. (This statement automatically calls the tostring method) 3.ps: function of tostring:
-Object in short, it is only a method specially added by Sun when developing Java to facilitate string operations of all classes. All objects have the tostring () method, because it is an existing method in the object, and all classes inherit the object, "all objects have this method"

-It is usually only for convenience of output, such as system. out. println (XX). If the "XX" in the brackets is not of the string type, the tostring () method of XX is automatically called. 4. the reflectiontostringbuilder class is used to implement the tostring () method in the class. It adopts the Java reflection mechanism (reflection) and uses the accessibleobject class in the reflection package to bypass access control and directly access the Private Members of the object. Therefore, when using this class, pay attention to the security policy of the runtime environment.

--------------------------------

After reading reflectiontostringbuilder, I found that there are too many output content. If you want to customize the content output by the tostring () method, you need to use the tostringbuilder and tostringstyle classes.
The append method in the tostringbuilder class can add basic types, arrays, and objects to the class. Only the added methods are output by the tostring method.
The tostringstyle class is used to format the output content.
Example:

Public String tostring (){
Return new tostringbuilder (this, tostringstyle. multi_line_style)
. Append ("name", name)
. Append ("Age", age)
. Tostring ();
}

For other functions, refer to the document of Jakarta commons.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.