Spring MVC: Reuse of Log Object logger

Source: Internet
Author: User

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

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.