Java reflection mechanism and Log4j implementation data

Source: Internet
Author: User

Based on the original design, the vertical expansion field design method can be used to get rid of the efficiency problem caused by a large number of access to the database in log records, is a feasible idea.

But is there a more concise way, such as getting rid of the database directly in the log record, using mature log4j to record the dynamic change history of the record data?

Let's take a look at it. In the framework of our company, most of the changes to data are implemented through the getXX () and setXX () methods, this gives us an inspiration. During the data modification process, if an object is running and the object's getXX method can be used to obtain the modified data of the object, then save. This idea is exciting and challenging. How can we get running objects during system operation?

The answer is the reflection mechanism of Java: the reflection mechanism is in the running state. For any class, all attributes and methods of the class can be known. For any object, can call any of its methods. This kind of information obtained dynamically and the function of calling methods of objects dynamically is called the reflection mechanism of java language.

The Code is as follows:

/***//**
* Using java reflection technology to record object-related parameter changes
* @ Author sparta 10/07/22
* @ Param obj: java object during runtime
* @ Param logger: log Component Object
* @ Return void
*/
Public static void log4Data (Object obj, Logger logger)
Throws SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {

// Get the runtime object
Class obj1 = obj. getClass ();

// Obtain all methods of the runtime object
Method [] methods = obj1.getMethods ();
String methodName = "";

// Record the parameters obtained by the method starting with get
For (Method method: methods ){

MethodName = (Member) method). getName ();

// Filter out methods that do not start with get and getClass methods that start with get.
If (methodName. indexOf ("get", 0) <0 | methodName. indexOf ("getlinoleic", 0)> = 0) continue;

Method getMethod = obj1.getMethod (methodName, null );
Logger.info ("::::::::::::" + methodName + "=" + getMethod. invoke (obj, null) + "::::::::::");
}
}

The call is as follows:

// Import related classes
Import org. apache. log4j. Logger;
Import *. *. User;
Import *. Util;

// Instantiate a log4j object
Logger logger = Logger. getLogger (this. getClass ());

// Instantiate and respond to the User class Builder (sex name and age)
User user = new User ("lxb", 32 );

// Call log4Data. The log4Data () method belongs to the Util class.
Util. log4Data (user, logger );

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.