When using the Spring Mvc+org.slf4j.logger development project, it was found that almost every controller or manager had a standard:
Private Final Static Logger Logger = Loggerfactory.getlogger (Test.class);
There seems to be no problem, but there is no doubt that every new controller or manager needs to write an almost identical code to reduce efficiency indirectly, because each controller or manager has one such log object, Will undoubtedly increase memory consumption, then is there any way to balance the performance and efficiency of the way?
If you dig a little deeper into the Java Foundation, you'll know one of the features of Java: polymorphism. In short, it allows a pointer to a base class or reference to an object of a derived class, and implements dynamic binding of the method at specific accesses .
The results are attached directly below:
1 @Controller 2 public Class Basecontroller { 3 public final Logger Logger = Loggerfactory.getlogger ( Span style= "COLOR: #0000ff" >this .getclass ()); 4 }
1 @Controller 2 @RequestMapping ("/test" 3 public class TestController extends Basecontroller { Span style= "COLOR: #008080" >4 @RequestMapping ("/" 5 public void Test () { 6 logger.error (" Log Object Logger multiplexing test "); 7 } 8 }
Simply explain, build a basic control layer Basecontroller, then create a new business controller, and inherit from Basecontroller, The logger log object in Basecontroller is used in a requestmapping in this business control layer to simply print the test statement.
After running, we will find that the log prints the object pointing to the TestController instead of the Basecontroller, that is, the Basecontroller in This.getclass () is referring to the sub-class TestController.
Thus, as long as the log object is defined in the Basecontroller, the subsequent business control layer does not need to define the log object every time, which is the effect of using polymorphism.
Of course, this will reduce the amount of code, but there will be disadvantages, that is, the original use of static definition of the log object, now not used, at the call method level by the static call into a dynamic call, the call speed will be lost .
If you find that there are any special problems, please correct me!
Spring MVC: Reuse of Log Object logger