0
votes

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

4

4 Answers

3
votes

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.

2
votes

I resolved my problem by adding dependancy 'spring-boot-starter-web'.

0
votes

My solution : In addition to the persistence.xml file I created application.properties file : # Connection url for the database spring.datasource.url=jdbc:postgresql://ip:port/db spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.hibernate.ddl-auto =update spring.jpa.show-sql = true chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL9Dialect spring.jpa.generate-ddl=true server.port = 9091

Afterwards, in my Application.java I added the following annotations and extend :

@SpringBootApplication
@EnableJpaRepositories("repositories")
@EntityScan("dao")
public class Application extends SpringBootServletInitializer {
-1
votes

You need to override configure method from SpringBootServletInitializer

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}