我有一个Spring Boot应用程序,需要:
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>
这是我的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); } }
在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
好像mvn spring-boot:run在运行main直接运行时不会发生的更多魔术。
mvn spring-boot:run
main
provided从spring-boot-starter-tomcat依赖关系中删除作用域可解决此问题,但当在servlet容器内运行战争时会引起麻烦。
provided
spring-boot-starter-tomcat
现在,我发现的唯一“修复”是mvn spring-boot:run在IntelliJ IDEA中运行,而不是直接运行main。尽管这是可以接受的解决方法,但我仍然想知道为什么它不起作用以及是否可以解决。
IntelliJ IDEA没有将provided依赖项注入到CLASSPATH中,正如Andy所说,这就是为什么spring无法创建嵌入式servlet容器的原因。
注释中提到的解决方法包括使用带有必要库的伪造模块,并将其用作类路径,使用-Xbootclasspath JVM参数,或使用自定义maven配置文件运行(compiled)与build(provided)。
(compiled)
build(provided)
步骤(IntelliJ 16): 1. Run-> Edit Configurations -> Add new configuration -> Pick Application type. 2. Set Main class to <your.main.class> 3. Set Use classpath of module to <*>_test (the test module!) 4. Ok and Run it!
Run
Edit Configuration
Add new configuration
Application
Set Main class
<your.main.class>
Use classpath of module
<*>_test
Ok