如何创建Java Mixins: @slf4j
我正在尝试创建Java Mixin并使用@Slf4j
注释。但是,Intellij显示了一个错误@slf4j仅适用于类和枚举
。
import lombok.extern.slf4j.Slf4j;
@Slf4j
public interface Foo {
default void foo() {
log.info("Hello world");
}
}
I'm trying to create a Java mixin and use the @Slf4j
annotation. However intellij shows an error @Slf4j is only legal for classes and enums
.
import lombok.extern.slf4j.Slf4j;
@Slf4j
public interface Foo {
default void foo() {
log.info("Hello world");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要使用记录界面的记录,则可以使用一个常数:
如果该界面具有某些实现,并且可以覆盖一个记录器:
使用Lombok,可以更简单地完成:
demo代码:
upputs:
If you want to use logging for the interface you could use a constant:
If will have some implementation for this interface and you could overwrite a logger:
With Lombok it could be done even simpler:
Demo code:
Output:
解决该问题的一种简单方法是将记录器传递到
foo()
方法:然后在实现
foo @slf4j
注释。 >::A simple way to solve the issue is to pass the logger to the
foo()
method:Then use the
@Slf4j
annotation on classes that implementFoo
:一个选项是将
getLogger
方法添加到接口,并要求实现该方法。然后接口可以调用该方法。One option is to add a
getLogger
method to the interface and require the implementations to implement that method. Then the interface can call that method.一种选项是使用静态
getLogger
方法创建内部类。One option is to create an inner class with a static
getLogger
method.