运行Spring Boot Headless

时间:2014-05-08 22:35:23

标签: spring-boot

我有一个弹簧启动应用程序,我想无头运行它。当我从终端运行时,这是我正在使用的命令:

java -jar myapp.jar --spring.main.headless = true

这是对的吗?任何帮助都非常感谢。

4 个答案:

答案 0 :(得分:8)

我遇到了同样的问题并使用SpringApplicationBuilder类解决了它。您应该将headless设置为false(默认情况下为true)。请参阅javadoc

我的主要方法如下:

  public static void main(String[] args) {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(App.class);
    builder.headless(false).run(args);
  }

答案 1 :(得分:1)

spring.main.*属性被注入SpringApplication。它对头部或无头无知。也许你需要java -Djava.awt.headless=true ...

答案 2 :(得分:1)

实际上SpringApplication确实支持无头模式[1],你所拥有的应该是有效的但是没有必要,因为true已经是默认值。

[1] https://github.com/spring-projects/spring-boot/blob/v1.0.2.RELEASE/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L691-L998

答案 3 :(得分:0)

您可以使用headless类来设置SpringApplicationBuilder。这是一个示例,说明如何从命令行开始或在Tomcat容器内部署战争。

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class TestApp extends SpringBootServletInitializer {

    private static Logger log = LogManager.getLogger(TestApp.class);

    /**
     * This method is executed when the App is loaded embedded in a Tomcat
     * @param builder
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        log.info("WAR IS STARTING..");
        builder.headless(false);
        return builder.sources(TestApp.class);
    }

    /**
     * This method is executed when the App is loaded by command line
     * @param args
     */
    public static void main(String[] args) {
        log.info("JAR IS STARTING...");
        SpringApplicationBuilder builder = new SpringApplicationBuilder(TestApp.class);
        builder.headless(false);
        builder.run(args);
    }

}