使用IDE运行Spring-boot的主要部分

Gui*_*Sim 33 java spring maven spring-boot

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

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

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

以下是我的pom.xml的有趣部分(请注意,我不从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>
Run Code Online (Sandbox Code Playgroud)

这是我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在IDE中运行main时,我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

看起来像是直接mvn spring-boot:run运行时不会发生的更多魔法main.

providedspring-boot-starter-tomcat依赖项中删除作用域修复了此问题,但在servlet容器内运行war时会导致问题.

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

yop*_*p83 25

https://youtrack.jetbrains.com/issue/IDEA-140041强烈启发的解决方法是使用测试类路径(包括嵌入式servlet)启动主类.

步骤(IntelliJ 16):

  1. Run- > Edit Configurations- > Add new configuration- >选择Application类型.
  2. 设置Main class<your.main.class>
  3. 设置Use classpath of module<*>_test(测试模块!)
  4. OkRun它!

  • 我没有_test模块. (16认同)
  • yop83或@Thirdman你能分享一下你如何获得测试模块吗? (3认同)

Mar*_*lin 15

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

IntelliJ IDEA没有将provided依赖项注入CLASSPATH,Andy表示这就是spring无法创建嵌入式servlet容器的原因.

自2005年以来,他们就此提出了一项功能要求:https://youtrack.jetbrains.com/issue/IDEABKL-99

注释中提到的变通方法包括使用带有必要库的假模块并将其用作类路径,使用-Xbootclasspath JVM参数或使用自定义maven配置文件进行running(compiled)vs building(provided).

  • 看起来Intellij IDEA 2018.1修复了这个问题.>"运行/调试配置"对话框中的应用程序和Spring Boot配置中有一个新的"包含"依赖项和"已提供"范围选项.此新功能允许您在需要时向类路径添加"提供的"依赖项.请注意,Spring Boot应用程序默认启用Include依赖项和"已提供"范围选项.https://blog.jetbrains.com/idea/2018/02/intellij-idea-2018-1-public-preview/ (7认同)
  • 请您放心一下如何在IntelliJ中实现解决方案?请问这个最简单的方法是什么? (4认同)

nis*_*yal 7

通过在Project structure-> Dependencies选项卡下将spring-boot-starter-tomcat依赖项的范围更改为"compile",我能够完成这项工作.这不会影响pom.xml,但允许此依赖项可用于spring boot run配置

点击此处查看关于在哪里更改此设置的图片


小智 7

我在使用 IntelliJ 2018 时遇到了同样的问题。最初,请确保您已在 IntelliJ 中为 spring 项目添加了 Maven 库。

我的解决办法是:

  1. 转到Run-> Edit Configurations

  2. 选择Application&& 选择您当前的项目。

  3. 检查Include dependencies with "Provided" scope

  4. OK -> RUN

  • 谢谢; 这很节省时间! (2认同)