Java 使用 IDE 运行 Spring-boot 的 main
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30237768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Spring-boot's main using IDE
提问by GuiSim
I have a spring-boot application that needs to:
我有一个 spring-boot 应用程序需要:
- Be deployable as a war in a servlet container
- Be runnable via `mvn spring-boot:run``
- 可部署为 servlet 容器中的战争
- 可以通过 `mvn spring-boot:run` 运行
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.
我还希望能够通过右键单击main
并运行它在我的 IDE(Eclipse 或 IntelliJ IDEA 社区)中运行该应用程序。
Here are the interesting parts of my pom.xml (Note that I do not inherit from spring-boot-starter-parent pom):
这是我的 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>
Here's my SpringBootServletInitializer
:
这是我的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);
}
}
When running the main inside an IDE I get the following error:
在 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
Seems like mvn spring-boot:run
does some more magic that does not happen when running the main
directly.
似乎mvn spring-boot:run
还有一些在main
直接运行时不会发生的魔法。
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.
provided
从spring-boot-starter-tomcat
依赖项中删除作用域可以解决此问题,但在 servlet 容器内运行 war 时会导致问题。
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.
现在我发现的唯一“修复”是mvn spring-boot:run
在 IntelliJ IDEA 中运行,而不是直接运行 main。虽然这是一种可以接受的解决方法,但我仍然想知道为什么这不起作用以及是否可以修复。
采纳答案by Marc-André Poulin
I believe this could be related to https://youtrack.jetbrains.com/issue/IDEA-107048
我相信这可能与https://youtrack.jetbrains.com/issue/IDEA-107048有关
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.
IntelliJ IDEA 没有将provided
依赖项注入到 CLASSPATH 中,正如 Andy 所说,这就是 Spring 无法创建嵌入式 servlet 容器的原因。
They have a feature request since 2005 about this: https://youtrack.jetbrains.com/issue/IDEABKL-99
自 2005 年以来,他们对此提出了一个功能请求:https: //youtrack.jetbrains.com/issue/IDEABKL-99
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
).
评论中提到的解决方法包括使用具有必要库的假模块并将其用作类路径、使用 -Xbootclasspath JVM 参数或使用自定义 maven 配置文件来运行 ( compiled
) 与构建 ( provided
)。
回答by Andy Wilkinson
mvn spring-boot:run
includes provided
dependencies when it's creating the classpath. It sounds like IntelliJ IDEA does not. Without Tomcat on the classpath, Spring Boot's unable to create an embedded servlet container which causes the exception you're seeing. Arguably this is a bug in IntelliJ as, if there's no container to provide the dependency, then it really needs to be on the classpath.
mvn spring-boot:run
provided
在创建类路径时包含依赖项。听起来 IntelliJ IDEA 没有。如果类路径上没有 Tomcat,Spring Boot 将无法创建嵌入式 servlet 容器,这会导致您看到的异常。可以说这是 IntelliJ 中的一个错误,因为如果没有容器来提供依赖项,那么它确实需要在类路径上。
You may be able to fix the problem by overriding the default classpath that IntelliJ uses when running the main method to include the spring-boot-starter-tomcat
dependency.
您可以通过覆盖 IntelliJ 在运行 main 方法以包含spring-boot-starter-tomcat
依赖项时使用的默认类路径来解决问题。
回答by yop83
A workaround that is strongly inspired from https://youtrack.jetbrains.com/issue/IDEA-140041is to start your main class with the test classpath (which includes the embedded servlet.)
受https://youtrack.jetbrains.com/issue/IDEA-140041启发的一种解决方法是使用测试类路径(包括嵌入式 servlet)启动主类。
Steps (IntelliJ 16):
步骤(IntelliJ 16):
Run
->Edit Configurations
->Add new configuration
-> PickApplication
type.- Set
Main class
to<your.main.class>
- Set
Use classpath of module
to<*>_test
(the test module!) Ok
andRun
it!
Run
->Edit Configurations
->Add new configuration
-> 选择Application
类型。- 设置
Main class
为<your.main.class>
- 设置
Use classpath of module
为<*>_test
(测试模块!) Ok
和Run
它!
回答by wang xinli
I find this page, and use the maven profile to manage the profiles.
我找到了这个页面,并使用 Maven 配置文件来管理配置文件。
<profiles>
<profile>
<id>PROD</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>DEV</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>TEST</scope>
</dependency>
</dependencies>
</profile>
</profiles>
and config the main class beforeLanuce
,set the command
并配置主类beforeLanuce
,设置命令
mvn clean compile -Pdev
回答by nishantkyal
I was able to make this work by changing the scope of the spring-boot-starter-tomcat dependency to "compile" under Project structure->Dependencies tab. This doesn't effect pom.xml but allows this dependencies to be available to spring boot run configuration
我能够通过在 Project structure->Dependencies 选项卡下将 spring-boot-starter-tomcat 依赖项的范围更改为“编译”来完成这项工作。这不会影响 pom.xml 但允许此依赖项可用于 spring boot 运行配置
Click here for image on where to change this setting in idea
回答by phn
I was able to work around this problem in Intellij IDEA 2017.2 by adding the provided libaray (spring-boot-starter-tomcat) to the project configuration.
通过将提供的 libaray (spring-boot-starter-tomcat) 添加到项目配置中,我能够在 Intellij IDEA 2017.2 中解决这个问题。
Select File -> Project Structure. Select Libraries and add a new project library (type = From Maven...). Search for spring-boot-starter-tomcat using the dialog, select the correct version and add it by clicking on OK. The library is added to the list of external libraries.
选择文件 -> 项目结构。选择 Libraries 并添加一个新的项目库(类型 = From Maven...)。使用对话框搜索 spring-boot-starter-tomcat,选择正确的版本并单击 OK 添加它。该库将添加到外部库列表中。
The disadvantage is that if the Spring Boot version is changed then you will have to remember to delete this library and add the new version.
缺点是如果更改了 Spring Boot 版本,则必须记住删除该库并添加新版本。
回答by AnthonyW
Using the profile and instructions below, you can add a profile to maven that allows development in IntelliJ without changing things for other environments.
使用下面的配置文件和说明,您可以向 maven 添加一个配置文件,允许在 IntelliJ 中进行开发,而无需更改其他环境的内容。
<!-- Leave original dependency as-is -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<!-- Enable this profile to run in IntelliJ. IntelliJ excludes provided dependencies from compile by default. -->
<profile>
<id>intellij</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Click the Maven Projectsbutton on the right side of IntelliJ, and under Profiles, select intellij
.
单击IntelliJ 右侧的Maven Projects按钮,然后在Profiles下选择intellij
。
回答by Sparow199
I had the same problem using IntelliJ 2018. My solution is:
我在使用 IntelliJ 2018 时遇到了同样的问题。我的解决方案是:
Go to
Run
->Edit Configurations
.Select
Application
&& choose your current project.Check
Include dependencies with "Provided" scope
.OK
->RUN
转到
Run
->Edit Configurations
。选择
Application
&& 选择您当前的项目。检查
Include dependencies with "Provided" scope
。OK
->RUN
回答by KayV
Follow these steps:
按着这些次序:
On the top right side of intellij window, click the drop down and select edit configuration and a new window will open.
In this window, on top left side, click "+" button and select sprint boot.
在intellij 窗口的右上角,单击下拉菜单并选择编辑配置,将打开一个新窗口。
在此窗口中,在左上角,单击“+”按钮并选择 sprint boot。