Preface
In our practical development, we often use reflection, I present an example of the logging function done by reflection in practical development.
Traditional logging
Some basic knowledge about reflection can be found in Reflection Primer.
I believe that logging is not new to you, in the actual development of some more sensitive data tables we need to record every operation of it.
First, let’s look at the traditional way of writing.
|
|
It is very simple to save the same data to the log table after saving the data table.
But this has several problems as follows.
- If the data table has more fields, for example hundreds of them. Then the
setter()
method of the log table will have to be written hundreds of times, and only if they are all correct. - If a field is added to the data table one day, the field will have to be added in every place where the log is written, raising the cost of maintenance.
Reflection is needed to solve the above situation.
Building logs using reflection
Let’s take a look at the changes to the code that result from using reflection.
|
|
The same save log, no matter how many fields, only three lines of code can be solved, and even if the fields change after writing the log this code still does not need to change.
In fact, the main method here is CommonUtil.setLogValueModelToModel(content, log);
Let’s see how this is done;
|
|
Before we can use it again we first need to build the main data table and then new
a log table object.
In the setLogValueModelToModel()
method.
- Get the class types of the data table and log table objects respectively.
- Get the set of all methods of the log object.
- Iterate through the collection and get the name of the method.
- Take only the method that starts with set in it, which is the set method. Because we need to assign a value to each field of the log object in the loop.
- After that, intercept the method name to get the specific field name.
- Using the previously intercepted field name, the
getMethod()
method returns thegetter
method for that field in the data table. - It is equivalent to executing
String content = content.getContent();
- Execute this method to get the specific value of the field.
- Use the
setter
method of the current loop to assign a value to each field of the log object. - It is equivalent to executing
log.setContent("asdsf");
where the field name is LogId
and the current loop is skipped because LogId is the primary key of the log table and does not need to be assigned a value.
When the loop ends, the log object is also constructed. After that, you just need to save it to the database.
Summary
Reflection is actually very resource-intensive and should be used with caution, and it is necessary to cache method, field, constructor and other objects.