I'm trying to start my spring-boot-web app but I'm getting the following error :
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
I configured the following ApplicationContext file :
@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;
}
}
I need those beans because in one of my services I inject the following objects :
@Autowired
private EntityManagerFactory emf;
@PersistenceContext
private EntityManager em;
and my main app :
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[]args)
{
//load the spring config class we configured
SpringApplication.run(AppConfig.class);
}
my dependencies in 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>
Solutions I tried but didnt work :
1)add extends SpringBootServletInitializer to both Application.java and Appconfig.java
2)Move the beans from the AppConfig.java to Application.java
3)Tried to set the following annotation on the application class :
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
4)Adding the following dependency to the pom.xml :
org.springframework.boot spring-boot-starter-tomcat 2.2.5.RELEASE
I resolved my problem by adding dependancy 'spring-boot-starter-web'.
Make sure that your filename, class name and the name of the class inside the SpringApplication.run() function are the same.
Also, make sure you haven't forgotten the @SpringBootApplication
annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With