gpt4 book ai didi

spring - 应用程序上下文异常: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

转载 作者:行者123 更新时间:2023-12-03 07:08:44 25 4
gpt4 key购买 nike

尝试设置一个简单的 Web 应用程序调度程序。这是我的配置:

    <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>scheduler</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.1.7.RELEASE</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

</dependencies>
<properties>
<start-class>hello.Application</start-class>
</properties>
<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>hello.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>

</pluginRepositories>
</project>

我的类(class)如下所示

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application implements WebApplicationInitializer {

public static void main(String[] args) throws Exception {
SpringApplication.run(ScheduledTasks.class);
}
public void onStartup(ServletContext arg0) throws ServletException {

SpringApplication.run(ScheduledTasks.class);

}

@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(8081);
factory.setSessionTimeout(50, TimeUnit.MINUTES);
return factory;
}
}
@EnableScheduling
public class ScheduledTasks {

@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
System.out.println("Testing successful ");
}
}

但是当我尝试启动容器时,我看到了异常

ERROR 2592 --- [ost-startStop-1] o.s.boot.SpringApplication               : Application startup failed
...
...
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:174)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:147)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:121)
... 20 more

我试图关注Spring Boot Testing: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean但徒劳无功。

请推荐,谢谢

最佳答案

您仅运行ScheduledTasks:

SpringApplication.run(ScheduledTasks.class);

什么时候应该运行:

SpringApplication.run(Application.class);

运行ScheduledTasks意味着Spring Boot不知道Application中的配置。至关重要的是,这意味着它看不到 @EnableAutoConfiguration(它是自动配置的开关),并且因为您的类路径上有 Tomcat,所以会创建嵌入式 Tomcat 实例。

我将通过将 @EnableScheduling 注释从 ScheduledTasks 移动到 Application 并运行 Application.class 来解决此问题而不是 ScheduledTasks.class,并使用 @Component 注释 ScheduledTasks:

应用程序类:

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableScheduling
public class Application implements WebApplicationInitializer {

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}

// …
}

ScheduledTasks.class:

@Component
public class ScheduledTasks {

@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
System.out.println("Testing successful ");
}
}

移动@EnableScheduling意味着它位于它所属的配置类上。运行 Application.class 意味着 Spring Boot 可以看到其所有配置,包括启用组件扫描和启用自动配置。用@Component注释ScheduledTasks表示通过组件扫描找到它,并且检测到它的@Scheduled方法。

这应该能让事情启动并运行。我还会更正您的 pom,以便您不会使用 Spring Boot 版本的混合(目前有 1.1.6 和 1.1.7)并删除您的 EmbeddedServletContainerFactory bean支持使用 src/main/resources 中的 application.properties 配置端口和 session 超时:

application.properties:

server.port = 8081
server.session-timeout = 3000

关于spring - 应用程序上下文异常: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com