Java Spring引导:由于缺少EmbeddedServletContainerFactory bean,无法启动EmbeddedWebApplicationContext
我对Spring完全陌生,开始在这个网站上做官方指南: 我想做以下指南: 我得到以下例外情况:Java Spring引导:由于缺少EmbeddedServletContainerFactory bean,无法启动EmbeddedWebApplicationContext,java,spring,spring-boot,Java,Spring,Spring Boot,我对Spring完全陌生,开始在这个网站上做官方指南: 我想做以下指南: 我得到以下例外情况: 2014-02-14 16:25:21.614 INFO 9032 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.scheduling.annotation.SchedulingConfiguration' of type [class org.springfr
2014-02-14 16:25:21.614 INFO 9032 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.scheduling.annotation.SchedulingConfiguration' of type [class org.springframework.scheduling.annotation.SchedulingConfiguration$$EnhancerByCGLIB$$5b48d763] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-02-14 16:25:21.638 INFO 9032 --- [ main] .c.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/work/Spring/SpringTutorial/target/classes/, file:/C:/work/apache-maven-3.0.3/repo/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter/1.0.0.RC1/spring-boot-starter-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot/1.0.0.RC1/spring-boot-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-autoconfigure/1.0.0.RC1/spring-boot-autoconfigure-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-logging/1.0.0.RC1/spring-boot-starter-logging-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jcl-over-slf4j/1.7.5/jcl-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jul-to-slf4j/1.7.5/jul-to-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/log4j-over-slf4j/1.7.5/log4j-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-core/1.0.13/logback-core-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-web/1.0.0.RC1/spring-boot-starter-web-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-tomcat/1.0.0.RC1/spring-boot-starter-tomcat-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-core/7.0.47/tomcat-embed-core-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-logging-juli/7.0.47/tomcat-embed-logging-juli-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-web/4.0.0.RELEASE/spring-web-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-webmvc/4.0.0.RELEASE/spring-webmvc-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-expression/4.0.0.RELEASE/spring-expression-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-databind/2.3.1/jackson-databind-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-annotations/2.3.0/jackson-annotations-2.3.0.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-core/2.3.1/jackson-core-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/commons-lang/commons-lang/2.2/commons-lang-2.2.jar]
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:140)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:658)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:355)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:920)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at hu.kumite.Application.main(Application.java:17)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:190)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:163)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
... 7 more
应用程序启动程序类如下所示:
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}
如您所见,main方法包含一个注释行。我已经完成了一个教程,即:
它正在运行。但我无法运行ScheduledTasks应用程序,如下所示:
@EnableScheduling
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("The time is now " + dateFormat.format(new Date()));
}
}
我使用Eclipse并将Application.java的main作为应用程序运行。
有人能帮帮我吗?日程安排指南不是一个web应用程序,所以您的pom.xml中可能有REST指南中的一些发霉的东西?如果您严格按照说明操作,它应该会起作用。您在上面发布的代码的另一个潜在问题是,您的
@EnableAutoConfiguration
类没有在上下文中使用,只是作为一个主要方法(这对于调度指南来说可能不是问题,但对于其他许多方法来说可能是问题)。试试这个
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}
对
@SpringBootApplication
的扫描显示,它包含以下注释:
@Configuration
@ComponentScan
@EnableAutoConfiguration
所以你也可以这样做:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}
加
org.springframework.boot
SpringBootStarterWeb
当main方法位于与传递给springapplication.run()的类不同的类上时,我遇到了类似的问题
因此,解决方案是使用您注释掉的行:
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在pom.xml中使用此选项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
或者这个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
org.springframework.boot
弹簧启动机tomcat
如果您使用命令行gradle bootRun
成功运行它,同时使用命令行gradle jar
将它打包到jar文件,以便使用命令行java-jar build/libs/demo.jar
运行它,不幸的是,由于缺少EmbeddedServletContainerFactory bean,因此无法启动EmbeddedWebApplicationContext,出现异常时失败:,在这种情况下,您需要使用gradle pluginspring boot
的任务bootRepackage
来生成特殊的可运行jar
- 设置1
$gradle清洁引导重新包装
- 设置2
$java-jarbuild/libs/demo.jar
如果您将其打包为一个jar,并且它是非web应用程序,请尝试加载应用程序上下文,如下所示
@SpringBootApplication
ApplicationContext ctx = new AnnotationConfigApplicationContext(Main.class);
或者使用下面的插件将其打包为一个jar
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
/
请注意,如果您将属性作为参数传递,则不包括@PropertySource(“classpath:test.properties”)
它将覆盖参数错误表明您尝试运行的应用程序无法实例化apache tomcat的实例。确保您正在使用tomcat运行应用程序
如果在检查所有依赖项后遇到相同的问题,请尝试在配置类中添加以下内容
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory();
return factory;
}
如果您使用的是tomcat的外部实例(特别是intellij),那么问题可能是IDE试图启动嵌入式tomcat。在这种情况下,从pom.xml中删除以下内容,然后使用“编辑配置”向导配置外部tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
org.springframework.boot
弹簧启动机tomcat
假如
ASpringApplication
将代表您尝试创建正确类型的ApplicationContext
。默认情况下,将使用AnnotationConfigApplicationContext
或AnnotationConfigEmbeddedWebApplicationContext
,具体取决于您是否正在开发web应用程序
用于确定“web环境”的算法相当简单(基于几个类的存在)。如果需要覆盖默认设置,可以使用setWebEnvironment(布尔webEnvironment)
还可以完全控制调用setApplicationContextClass(…)代码>
[提示]
在JUnit测试中使用SpringApplication
时,通常需要调用setWebEnvironment(false)
。我在一个Spring Boot项目中有多个应用程序类,其中包括启动的web,并希望避免为其中一个配置web环境,因此我手动将其配置如下:
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
new SpringApplicationBuilder(Application.class)
.web(false)
.run(args);
}
}
针对Spring Boot 2及以上版本更新:
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.NONE)
.run(args);
}
}
如果您在使用intellij时遇到此异常,并且您正试图使用run
按钮启动应用程序。请尝试从命令行启动应用程序。例如,确保您位于正确的目录(包含pom文件的目录)假设这是一个springboot
应用程序运行mvn-springboot:run
这对我来说是个好办法
此外,我还看到,当您的spring应用程序依赖于另一个应用程序时,会发生此错误。在这种情况下,我必须先启动另一个应用程序,然后再运行。在starter类修复此问题之前添加注释@SpringBootApplication
(因此,本质上,此错误消息可能意味着“您没有@springbootplication
标记的类,您至少需要一个)
添加spring启动程序依赖项修复了我的错误
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
如果您想将tomcat作为嵌入式服务器启动,这是必需的。在我的例子中,我们新添加了@Profile注释,以便忽略生产模式下的TestApplication类和测试模式下的Application类
很遗憾,我们忘记将以下行添加到application.properties文件中:
spring.profiles.active=test
or
spring.profiles.active=production
如果没有这些配置文件,则不会加载任何配置文件,这会导致“不太会说话”的Spring错误。这通常是由依赖性问题引起的
@SpringBootApplication
public class AppStarter {
public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring.profiles.active=test
or
spring.profiles.active=production
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
//SpringApplication.run(Application.class, args);
SpringApplication.run(ScheduledTasks.class, args);
}
}
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<properties>
<java.version>1.8</java.version>
<!-- The main class to start by executing java -jar -->
<start-class>com.scmaer.java.microservice.Application</start-class>
<cxf.version>3.1.5</cxf.version>
<olingo.version>2.0.10</olingo.version>
<spring.boot.version>1.4.7.RELEASE</spring.boot.version>
<spring.boot.plugin.version>1.5.8.RELEASE</spring.boot.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>false</skipTests>
</properties>
@SpringBootApplication
public class Main {
public static void main(String args[]){
SpringApplication.run(Main.class, args);
}
}
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>*/*.properties</exclude>
</excludes>
</resource>
</resources>
mvn dependency:purge-local-repository
@SpringBootApplication
public class LoginSecurityAppApplication {
public static void main(String[] args) {
SpringApplication.run(LoginSecurityAppApplication.class, args);
}
}
start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>