Difference getLog (getClass ()) and getLog (XXX.class) of

Log in reference example method, generally define an instance variable:

// 在实例方法中引用Log:
public class Person {
    protected final Log log = LogFactory.getLog(getClass());

    void foo() {
        log.info("foo");
    }
}

Notes that instance variable log acquisition mode is LogFactory.getLog (getClass ()), although you can also use LogFactory.getLog (Person.class), but the former approach has a very big advantage is that you can directly use the subclass log instance. E.g:

// 在子类中使用父类实例化的log:
public class Student extends Person {
    void bar() {
        log.info("bar");
    }
}

Due to the dynamic characteristics of Java classes, log fields subclass acquired is actually equivalent LogFactory.getLog (Student.class), but it is inherited from the parent class, and no need to change the code.

Source: https://www.liaoxuefeng.com/wiki/1252599548343744/1264738932870688

Guess you like

Origin www.cnblogs.com/cag2050/p/11448078.html