Gui*_*Sim 33 java spring maven spring-boot
我有一个spring-boot应用程序需要:
我还希望能够通过右键单击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.
provided从spring-boot-starter-tomcat依赖项中删除作用域修复了此问题,但在servlet容器内运行war时会导致问题.
现在我发现的唯一"修复"是mvn spring-boot:run在IntelliJ IDEA中运行而不是直接运行main.虽然这是一个可接受的解决方法,但我仍然想知道为什么这不起作用以及是否可以修复.
yop*_*p83 25
从https://youtrack.jetbrains.com/issue/IDEA-140041强烈启发的解决方法是使用测试类路径(包括嵌入式servlet)启动主类.
Run- > Edit Configurations- > Add new configuration- >选择Application类型.Main class为<your.main.class>Use classpath of module为<*>_test(测试模块!)Ok而Run它!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).
通过在Project structure-> Dependencies选项卡下将spring-boot-starter-tomcat依赖项的范围更改为"compile",我能够完成这项工作.这不会影响pom.xml,但允许此依赖项可用于spring boot run配置
小智 7
我在使用 IntelliJ 2018 时遇到了同样的问题。最初,请确保您已在 IntelliJ 中为 spring 项目添加了 Maven 库。
我的解决办法是:
转到Run-> Edit Configurations。
选择Application&& 选择您当前的项目。
检查Include dependencies with "Provided" scope。
OK -> RUN
| 归档时间: |
|
| 查看次数: |
43691 次 |
| 最近记录: |