gpt4 book ai didi

java - spring-boot web应用程序无法启动: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

转载 作者:行者123 更新时间:2023-12-01 17:55:16 37 4
gpt4 key购买 nike

我正在尝试启动我的 spring-boot-web 应用程序,但收到以下错误:

2020-03-17 20:54:22.643  WARN 15640 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
2020-03-17 20:54:22.664 ERROR 15640 --- [ main] o.s.boot.SpringApplication : Application run failed

我配置了以下 ApplicationContext 文件:

@Configuration
@EnableJpaRepositories(basePackages = {"repos"})
public class AppConfig {
@Bean
public LocalEntityManagerFactoryBean entityManagerFactory() {
LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
factoryBean.setPersistenceUnitName("mydb");

return factoryBean;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);

return transactionManager;
}
}

我需要这些 bean,因为在我的一项服务中我注入(inject)了以下对象:

@Autowired
private EntityManagerFactory emf;

@PersistenceContext
private EntityManager em;

和我的主应用程序:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {


public static void main(String[]args)
{
//load the spring config class we configured
SpringApplication.run(AppConfig.class);
}

我在 pom.xml 中的依赖项:

  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.11</version>
</dependency>

我尝试过但不起作用的解决方案:

1)添加将SpringBootServletInitializer扩展至Application.java和Appconfig.java

2)将bean从AppConfig.java移动到Application.java

3)尝试在应用程序类上设置以下注释:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

4)将以下依赖项添加到 pom.xml :

org.springframework.boot spring-boot-启动器-tomcat 2.2.5.发布

最佳答案

我通过添加依赖项“spring-boot-starter-web”解决了我的问题。

关于java - spring-boot web应用程序无法启动: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60728683/

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