SpringBoot CommandLineRunner run()方法未被调用

我正在学习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;


@SpringBootApplication

public 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);

}


@Override

public 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()


我敢肯定我犯了一个简单的错误,但是我看不到它。任何帮助表示赞赏。


大话西游666
浏览 1112回答 3
3回答

www说

首先,调用即将进入您的run方法。但是不记录任何内容,因为记录器存在一些问题。您可以System.out.println(...)用来确认这一点。如下更改您的Logger声明。private static final Logger logger = LoggerFactory.getLogger(DvMain.class);您没有使用正确的库。从slf4j使用Logger而不是log4jimport org.slf4j.Logger; import org.slf4j.LoggerFactory;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java