我正在学习SpringBoot。我的(琐碎的)问题是我无法从SpringBoot调用的CommandLineRunner接口中获取run()方法。我正在使用Java 8,Eclipse Oxygen,Maven,并且正在将我的项目作为“ Spring Boot应用程序”运行。代码是:package com.clarivate.dataviewer;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DvMain implements CommandLineRunner{static Logger logger = LogManager.getRootLogger();public static void main(String[] args) { logger.debug("DS1A in main()"); //SpringApplication.run(DvMain.class, args); SpringApplication app = new SpringApplication(DvMain.class); //ConfigurableApplicationContext app = SpringApplication.run(DvMain.class, args); logger.debug("DS1B in main()"); app.run(args);}@Overridepublic void run(String... args) throws Exception { // This does not get called logger.debug("DS4 this line is never printed");}}被覆盖的run()方法不会被调用。我尝试创建一个单独的类来实现CommandLineRunner,但存在相同的问题。控制台跟踪为:SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/C:/Users/44/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/C:/Users/44/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]12:58:42.225 [main] DEBUG - DS1A in main()12:58:42.289 [main] DEBUG - DS1B in main()我敢肯定我犯了一个简单的错误,但是我看不到它。任何帮助表示赞赏。
3 回答
![?](http://img1.sycdn.imooc.com/545847f50001126402200220-100-100.jpg)
www说
TA贡献1775条经验 获得超8个赞
首先,调用即将进入您的run方法。但是不记录任何内容,因为记录器存在一些问题。您可以System.out.println(...)
用来确认这一点。
如下更改您的Logger声明。
private static final Logger logger = LoggerFactory.getLogger(DvMain.class);
您没有使用正确的库。从slf4j使用Logger而不是log4j
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
添加回答
举报
0/150
提交
取消