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.

10
  • 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. Commented May 9, 2016 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. Commented May 9, 2016 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. Commented May 9, 2016 at 11:37
  • Thank you very much for the [email protected] Commented May 9, 2016 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). Commented May 9, 2016 at 12:20

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

These possible solution i went through but it didnt work out.
It seems to me that the problem might be in your pom file. As mentioned in other comments, make sure the spring jar files are all the same version and that you have added all jars that you need
which one is line 13 in your Application class?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.