JeyJ
JeyJ

Reputation: 4070

spring-boot web app fails to start : Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

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

Upvotes: 4

Views: 41499

Answers (7)

Ajay Takur
Ajay Takur

Reputation: 6236

Add 3.1.1 in to properties like below than fix issue

<properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

Just Update Project => right click => Maven=> Update Project

Upvotes: 0

Andrey Lebedenko
Andrey Lebedenko

Reputation: 1988

I was fixing this because of https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1924298

And my fix was to do this in pom.xml only:

<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>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>

No changes in classes or config were needed.

Upvotes: 1

Muhammed Fasil
Muhammed Fasil

Reputation: 8566

You can comment the <scope>provided</scope> in pom.xml file like below

See the sample snippet belo

Upvotes: 1

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

Upvotes: 4

Ripunjoy RM Medhi
Ripunjoy RM Medhi

Reputation: 31

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.

Upvotes: 3

abharku
abharku

Reputation: 145

You need to override configure method from SpringBootServletInitializer

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

Upvotes: -2

JeyJ
JeyJ

Reputation: 4070

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 {

Upvotes: 0

Related Questions