将 SLF 与 Java9 模块一起使用
2022-09-02 22:42:26
我使用java 9模块设置了一个小型测试项目。结构如下所示:
.
├── build.gradle
└── src
├── main
│ └── java
│ ├── module-info.java
│ └── slfTest
│ └── Main.java
└── test
└── java
└── slfTest
└── MainTest.java
(随意克隆并自己看看:git clone https://github.com/michas2/slfTest.git
)
类 Main 和 Main Test 只记录一些简单的输出:
Logger logger = LoggerFactory.getLogger(Main.class);
logger.info("Hello World");
现在按预期工作,但给出了 ClassCastException。gradle run
gradle test
$ gradle run -q
[main] INFO slfTest.Main - Hello World
$ gradle test -q
java.lang.ClassCastException: org.slf4j.simple/org.slf4j.simple.SimpleLoggerFactory cannot be cast to org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext
at org.gradle.internal.logging.slf4j.Slf4jLoggingConfigurer.configure(Slf4jLoggingConfigurer.java:42)
at org.gradle.internal.logging.config.LoggingSystemAdapter.startCapture(LoggingSystemAdapter.java:54)
at org.gradle.internal.logging.services.DefaultLoggingManager$StartableLoggingSystem.start(DefaultLoggingManager.java:297)
at org.gradle.internal.logging.services.DefaultLoggingManager.start(DefaultLoggingManager.java:73)
at org.gradle.internal.logging.services.DefaultLoggingManager.start(DefaultLoggingManager.java:37)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:83)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:64)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:62)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:67)
删除java 9模块可以使事情再次工作。因此,我假设存在模块访问问题。- 修复它的正确方法是什么?
内容是:module-info.java
module slfTest {
requires org.slf4j;
exports slfTest;
}
当 gradle 运行测试时,gradle 会尝试注入自己的日志记录后端。- 猜猜这是出现一些模块访问问题的部分。