我有一个内部类,我想添加一个记录器,目前正在通过构造函数添加它。
我的外部类(spring bean)具有以下内容:
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
我的内部课程有以下几点:
private final static class car {
final Logger LOGGER;
public car(Logger LOGGER) {
this.LOGGER = LOGGER;
}
}
我不喜欢上面有没有其他方法可以解决这个问题?
答案 0 :(得分:2)
你可以这样做:
public class Foo
{
private final Logger logger = LoggerFactory.getLogger (getClass ());
private static class Bar
{
private final Logger logger = Foo.this.logger;
}
}
但更好的办法可能是:
public class Foo
{
private final static Logger logger = LoggerFactory.getLogger (Foo.class);
private static class Bar
{
private final static Logger logger = LoggerFactory.getLogger (Bar.class);
}
}
答案 1 :(得分:0)
private final static Logger LOGGER = LoggerFactory.getLogger(getClass());
private final static class Car {
void drive() {
LOGGER.debug(msg);
}
}
或
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
private final static class Car {
private final Logger LOGGER = LoggerFactory.getLogger(Car.class);
void drive() {
LOGGER.debug(msg);
}
}
答案 2 :(得分:0)
如果您的意图是分享,您可以要求提供给外部课程的同一个记录器:
private final Logger LOGGER = LoggerFactory.getLogger(OuterClass.class);