title: Failed to load ApplicationContext
body: <p>Currently I'm setting-up a Spring MVC + Hibernate + Maven Web Application. </p>
  
  <p>Im stack with a JUnit error forbidding the run of my application with tomcat</p>
  
  <p>Here is the JUnit failure trace :</p>
  
  <pre><code>  java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
      at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
      at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.service.TeamService com.amadeus.webAppDemo.controller.TeamController.teamService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.service.TeamService com.amadeus.webAppDemo.controller.TeamController.teamService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.amadeus.webAppDemo.dao.TeamDao com.amadeus.webAppDemo.service.TeamServiceImpl.teamDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.amadeus.webAppDemo.dao.TeamDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.hibernate4.LocalSessionFactoryBean com.amadeus.webAppDemo.init.WebAppConfig.sessionFactory()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/amadeus/webAppDemo/init/WebAppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource com.amadeus.webAppDemo.init.WebAppConfig.dataSource()] threw exception; nested exception is java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  
  Caused by: java.lang.IllegalStateException: required key [com.mysql.jdbc.Driver] not found
  </code></pre>
  
  <p>For test I have two classes. The first one is : BaseTestConfig.java :</p>
  
  <pre><code>@Configuration
  @ComponentScan("com.mycompany.webAppDemo")
  @EnableWebMvc
  public class BaseTestConfig {
  
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/pages/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  
  }
  </code></pre>
  
  <p>The second one is LinkControllerClasses.java :</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  @ContextConfiguration(classes=BaseTestConfig.class)
  public class LinkControllerTest {
  
      @Autowired
      private WebApplicationContext wac;
  
      private MockMvc mockMvc;
  
      @Before
      public void init() {
          mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
      }
  
      @Test
      public void testHomePage() throws Exception {
          mockMvc.perform(get("/"))
              .andExpect(status().isOk())
              .andExpect(view().name("home"));
      }
  
      @Test
      public void testIndexPage() throws Exception {
          mockMvc.perform(get("/index.html"))
              .andExpect(status().isOk())
              .andExpect(view().name("home"));
      }
  
  }
  </code></pre>
  
  <p>For my application configuration I have two classes :
   Initializer.java </p>
  
  <pre><code>public class Initializer implements WebApplicationInitializer{
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          // TODO Auto-generated method stub
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(WebAppConfig.class);
          servletContext.addListener(new ContextLoaderListener(ctx));
  
          ctx.setServletContext(servletContext);
  
          Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          servlet.addMapping("/");
          servlet.setLoadOnStartup(1);
      }
  }
  </code></pre>
  
  <p>And WebAppConfig.java :</p>
  
  <pre><code>@Configuration
  @ComponentScan("com.mycompany.webAppDemo")
  @EnableWebMvc
  @EnableTransactionManagement
  @PropertySource("classpath:application.properties")
  public class WebAppConfig {
  
      private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
      private static final String PROPERTY_NAME_DATABASE_PASSWORD = "";
      private static final String PROPERTY_NAME_DATABASE_URL = "jdbc:mysql://127.0.0.1:3306/demodb";
      private static final String PROPERTY_NAME_DATABASE_USERNAME = "root";
  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQL5InnoDBDialect";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true";
      private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "com.amadeus.webAppDemo.model";
  
      @Resource
      private Environment env;
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
          dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
          dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
          dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
          dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
  
          return dataSource;
      }
  
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
          sessionFactoryBean.setDataSource(dataSource());
          sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
          sessionFactoryBean.setHibernateProperties(hibProperties());
          return sessionFactoryBean;
      }
  
      private Properties hibProperties() {
          Properties properties = new Properties();
          properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
          properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
          return properties;  
      }
  
      @Bean
      public HibernateTransactionManager transactionManager() {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(sessionFactory().getObject());
          return transactionManager;
      }
  
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/pages/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  
  }
  </code></pre>
  
  <p>Finaly here is the two controller classes :</p>
  
  <p>LinkControl.java</p>
  
  <pre><code>@Controller
  public class LinkController {
  
       @RequestMapping(value="/")  
          public ModelAndView mainPage() {  
              return new ModelAndView("home");  
          }  
  
       @RequestMapping(value="/index")  
          public ModelAndView indexPage() {  
              return new ModelAndView("home");  
          } 
  
  }
  </code></pre>
  
  <p>TeamController.java</p>
  
  <pre><code>@Controller
  public class TeamController {
  
      @Autowired
      private TeamService teamService; 
  
      @RequestMapping(value="/team/add")
      public ModelAndView addTeamPage(){
          ModelAndView modelAndView = new ModelAndView("add-team-form");
          modelAndView.addObject("team", new Team());
          return modelAndView;
      }
  
      @RequestMapping(value="/team/add/process")
      public ModelAndView addingTeam(@ModelAttribute Team team){
          ModelAndView modelAndView = new ModelAndView("home");
          teamService.addTeam(team);
  
          String message ="Team was successfully added";
          modelAndView.addObject("message", message); 
  
          return modelAndView;  
      }
  
      @RequestMapping(value="/team/list")  
      public ModelAndView listOfTeams() {  
          ModelAndView modelAndView = new ModelAndView("list-of-teams");  
  
          List&lt;Team&gt; teams = teamService.getTeams();  
          modelAndView.addObject("teams", teams);  
  
          return modelAndView;  
      }
  
        @RequestMapping(value="/team/edit/{id}", method=RequestMethod.GET)  
          public ModelAndView editTeamPage(@PathVariable Integer id) {  
              ModelAndView modelAndView = new ModelAndView("edit-team-form");  
              Team team = teamService.getTeam(id);  
              modelAndView.addObject("team",team);  
              return modelAndView;  
          }  
  
        @RequestMapping(value="/team/edit/{id}", method=RequestMethod.POST)  
          public ModelAndView edditingTeam(@ModelAttribute Team team, @PathVariable Integer id) {  
  
              ModelAndView modelAndView = new ModelAndView("home");  
  
              teamService.updateTeam(team);  
  
              String message = "Team was successfully edited.";  
              modelAndView.addObject("message", message);  
  
              return modelAndView;  
          }  
  
        @RequestMapping(value="/team/delete/{id}", method=RequestMethod.GET)  
          public ModelAndView deleteTeam(@PathVariable Integer id) {  
              ModelAndView modelAndView = new ModelAndView("home");  
              teamService.deleteTeam(id);  
              String message = "Team was successfully deleted.";  
              modelAndView.addObject("message", message);  
              return modelAndView;  
          }  
  }
  </code></pre>
  
  <p>I tried the suggested modification of a similar issue
  <a href="https://stackoverflow.com/questions/12261899/failed-to-load-applicationcontext-with-contextconfigurationclasses">Failed to load ApplicationContext with @ContextConfiguration(classes={ ... })</a> </p>
  
  <p>But it is not working :(</p>
  
  <hr>
  
  <p>Thank you for your answer here is the application.properties</p>
  
  <pre><code>#DB properties:
  db.driver=com.mysql.jdbc.Driver
  db.url=jdbc:mysql://localhost:3306/demodb
  db.username=root
  db.password=*****
  
  #Hibernate Configuration:
  hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  hibernate.show_sql=true
  entitymanager.packages.to.scan=com.mycompany.webAppDemo.model
  </code></pre>
  
  <p>I made some change in my WebAppConfig.java.For eg.instead of :</p>
  
  <pre><code>private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  </code></pre>
  
  <p>I do this :</p>
  
  <pre><code>private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
  </code></pre>
  
  <p>But still having the same error :(</p>