使用 IDE 运行 Spring-boot 的 main [英] Run Spring-boot's main using IDE

查看:29
本文介绍了使用 IDE 运行 Spring-boot 的 main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 spring-boot 应用程序需要:

I have a spring-boot application that needs to:

  • 可部署为 servlet 容器中的战争
  • 可以通过 `mvn spring-boot:run` 运行

我还希望能够在我的 IDE(Eclipse 或 IntelliJ IDEA 社区)中通过右键单击 main 并运行它来运行该应用程序.

I'd also like to be able to run this application in my IDE (Eclipse or IntelliJ IDEA Community) by right clicking on the main and running it.

这里是我的 pom.xml 中有趣的部分(注意我不是从 spring-boot-starter-parent pom 继承的):

Here are the interesting parts of my pom.xml (Note that I do not inherit from spring-boot-starter-parent pom):

...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
...
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

这是我的SpringBootServletInitializer:

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.company.theproject")
public class Application extends SpringBootServletInitializer
{
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(Application.class);
    }

    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

在 IDE 中运行 main 时,出现以下错误:

When running the main inside an IDE I get the following error:

org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
    ... 12 common frames omitted

似乎 mvn spring-boot:run 做了一些在直接运行 main 时不会发生的魔法.

Seems like mvn spring-boot:run does some more magic that does not happen when running the main directly.

spring-boot-starter-tomcat 依赖项中移除 provided 作用域可以解决这个问题,但在 servlet 容器内运行 war 时会导致问题.

Removing the provided scope from the spring-boot-starter-tomcat dependency fixes this issue but causes trouble when the war is run inside a servlet container.

现在我发现的唯一修复"是在 IntelliJ IDEA 中运行 mvn spring-boot:run 而不是直接运行 main.虽然这是一种可以接受的解决方法,但我仍然想知道为什么这不起作用以及是否可以修复.

Right now the only "fix" I've found is to run mvn spring-boot:run within IntelliJ IDEA instead of running the main directly. While this is an acceptable workaround, I'd still like to know why this doesn't work and if it can be fixed.

推荐答案

我相信这可能与 https://有关youtrack.jetbrains.com/issue/IDEA-107048

IntelliJ IDEA 没有将 provided 依赖项注入到 CLASSPATH 中,正如 Andy 所说,这就是 Spring 无法创建嵌入式 servlet 容器的原因.

IntelliJ IDEA is not injecting the provided dependencies into the CLASSPATH and as Andy stated this is why spring is unable to create the embedded servlet container.

自 2005 年以来,他们有一个关于此的功能请求:https://youtrack.jetbrains.com/issue/IDEABKL-99

They have a feature request since 2005 about this: https://youtrack.jetbrains.com/issue/IDEABKL-99

评论中提到的解决方法包括使用具有必要库的假模块并将其用作类路径、使用 -Xbootclasspath JVM 参数或使用自定义 maven 配置文件来运行(compiled)与构建(<代码>提供).

Workarounds mentioned in the comments includes having a fake module with the necessary libs and using it as classpath, using the -Xbootclasspath JVM argument or using custom maven profiles for running (compiled) vs building (provided).

这篇关于使用 IDE 运行 Spring-boot 的 main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆