1

I am getting this error when i run my spring application:

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:124)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at com.dh.test.Application.main(Application.java:13)
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)
    ... 7 more

My Main Class looks like:

@Configuration
@ComponentScan
public class Application{

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

}

I am trying to configure the properties of spring through java classes using @Bean:

@Configuration
@EnableJpaRepositories(
            entityManagerFactoryRef = "customerEntityManager",
            transactionManagerRef = "customerTransactionManager",
            basePackages = {"com.dh.test.repository.customer"})
public class CustomerDbConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] {"com.dh.test.model.customer"});
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalJpaProperties());
        em.setPersistenceUnitName("customers");
        return em;
    }

    Properties additionalJpaProperties(){
        Properties properties = new Properties();
        properties.setProperty("hibernate.ddl-auto", "update");
        properties.setProperty("jdbc.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.setProperty("hibernate.show-sql", "true");

        return properties;
    }

    @Bean
    public DataSource dataSource(){
        return DataSourceBuilder.create()
                .url("jdbc:mysql://localhost:3306/customer")
                .driverClassName("com.mysql.jdbc.Driver")
                .username("root")
                .password("root")
                .build();
    }   

    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory customerEntityManager){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(customerEntityManager);
        return transactionManager;
    }
}

extract of my POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>boot-multidb-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>boot-multidb-sample</name>
    <description>Spring Boot Multiple Database Configuration</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>

        <!-- Datasource and connection pool dependencies -->
        <!-- <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> 
            </dependency> -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.33</version>
        </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jpa</artifactId>
        <version>2.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Can anyone please give a possible solution to fix this error, I tried checking the dependency hirerachy and the dependencies in my project.

Deepanjan
  • 649
  • 2
  • 8
  • 15
  • You are using Spring Boot then use spring boot. Why are you configuring everything yourself, while spring boot already does that for you (basically you could remove your whole configuration class). Also your application class is missing `@EnableAutoConfiguration` or replace all 3 annotations with a single `@SpringBootApplication` and add `spring-boot-starter-web` as a dependency. – M. Deinum May 09 '16 at 11:19
  • I am trying to create a layer in jpa to generate separate schemas in the database that's why i am configuring evrything myself. – Deepanjan May 09 '16 at 11:28
  • Still you can use Spring Boot for that. Your current is overriding the defaults an you only have a single transaction manager. As long as you don't have multiple entity managers you don't need to do such a thing. Main point is the wrong `Application` class (missing annotation) and the fact that there is a missing dependency. – M. Deinum May 09 '16 at 11:37
  • Thank you very much for the solution.@M.Deinum – Deepanjan May 09 '16 at 12:19
  • Also your `spring-jpa` dependency is old, never mix versions of a framework, remove that dependency (as that is already managed by Spring for you). – M. Deinum May 09 '16 at 12:20
  • Yes, later i found that there is a mix versions of a famework so i managed it accordingly to the requirement. – Deepanjan May 09 '16 at 12:22
  • After fixing all the problems as mentioned by you still it is showing this exception: **org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.addApplicationListener(org.apache.catalina.deploy.ApplicationListener)** – Deepanjan May 09 '16 at 12:33
  • Looks like you have incompatible versions of jars. – M. Deinum May 09 '16 at 13:12
  • i cross checked the version of jars. – Deepanjan May 10 '16 at 04:57
  • Add your updated pom and the output of `mvn dependency:tree`. – M. Deinum May 10 '16 at 06:04

2 Answers2

1

It seems to me that the EmbeddedServletContainerFactory bean is not initialized. You need to kick it off:

Add the @SpringBootApplication annotation to your Application class

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
       SpringApplication.run(ScheduledTasks.class, args);
    }
}

The @SpringBootApplication annotation contains

  • @Configuration
  • @ComponentScan
  • @EnableAutoConfiguration (this is the one that might fix your problem)

Also you might need to add spring-boot-starter-web dependency in your pom:

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

Sources: Spring boot error :Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

Community
  • 1
  • 1
Viorel Florian
  • 594
  • 9
  • 29
0

in Your class named Application ( i.e in the class where you write SpringApplication.run("...") you have to import org.springframework.boot.autoconfigure.SpringBootApplication for the annotation @SpringBootApplication.

If you import import org.springframework.boot.SpringBootConfiguration then you will get that exception.

kohane15
  • 809
  • 12
  • 16
Srikant M
  • 35
  • 2