title: No bean named 'entityManagerFactory' available
body: <p>I created a Spring Boot application and I created a class with the methods I want to be executed. When deploying the project as a war file, I am getting the errors from the stacktrace. I want to run the method from class TennisExecutor.</p>
  
  <ul>
  <li>No bean named 'entityManagerFactory' available</li>
  <li>Error creating bean with name '(inner bean)#366583f9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available</li>
  </ul>
  
  <p>I will show some parts of my code:</p>
  
  <pre><code>
  @Configuration
  @EnableAsync
  @EnableScheduling
  @ComponentScan(basePackages={"parser", "service", "processing", "automation"})
  public class TennisExecutor extends SpringBootServletInitializer {
  
      @Autowired
      @Qualifier("tennisDataSource")
      private DataSource dataSource;
  
      @Autowired
      @Qualifier("tennisTm")
      PlatformTransactionManager transactionManager;
  
      @Autowired
      public ParseUpcomingMatchesFile parseUpcomingMatchesFile;
  
      @Autowired
      public ParseFinishedMatchesFile parseFinishedMatchesFile;
  
      @Autowired
      public PlayersProcessor playersProcessor;
  
      public TennisExecutor() {}
  
      @Scheduled(cron = "0 */2 * * * ?")
      public void executeTasks() throws IOException {
  
           parseUpcomingMatchesFile.parseMatchesFile(webDriver, new File(ConstantData.TOMORROW_UPCOMING_MATCHES_FILE_PATH));
  
       }
  
  </code></pre>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(basePackageClasses = {ParseUpcomingMatchesFile.class, ParseFinishedMatchesFile.class,
          PlayersProcessor.class}, entityManagerFactoryRef = "tennisEmf", transactionManagerRef = "tennisTm")
  @EnableTransactionManagement
  class ApplicationConfig {
  
      @Bean
      public EntityManagerFactoryBuilder builder(Environment environment) {
          Map&lt;String, String&gt; jpaProperties = new HashMap&lt;&gt;();
          jpaProperties.put("hibernate.show_sql", environment.getProperty("spring.jpa.show-sql"));
          jpaProperties.put("hibernate.format_sql", environment.getProperty("spring.jpa.properties.hibernate.format_sql"));
          jpaProperties.put("hibernate.dialect", environment.getProperty("spring.jpa.properties.hibernate.dialect"));
          return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), jpaProperties, null);
      }
  
      @Bean(name = "tennisDataSource")
      public DataSource dataSource(Environment environment) {
  
          HikariDataSource dataSource = new HikariDataSource();
          dataSource.setJdbcUrl(environment.getRequiredProperty("spring.datasource.url"));
          dataSource.setUsername(environment.getRequiredProperty("spring.datasource.username"));
          dataSource.setPassword(environment.getRequiredProperty("spring.datasource.password"));
          return dataSource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean tennisEmf(@Qualifier("tennisDataSource") DataSource tennisDataSource, EntityManagerFactoryBuilder builder) {
          return builder.dataSource(tennisDataSource).packages(ParseUpcomingMatchesFile.class, ParseFinishedMatchesFile.class, PlayersProcessor.class).persistenceUnit("tennisPersistenceUnit").build();
      }
  
      @Bean
      public JpaTransactionManager tennisTm(@Qualifier("tennisEmf") EntityManagerFactory emf) {
          return new JpaTransactionManager(emf);
      }
  }
  </code></pre>
  
  <pre><code>@Component
  @Transactional(value = "tennisTm")
  @EnableJpaRepositories(basePackageClasses={MatchesRepository.class, PlayersRepository.class}, entityManagerFactoryRef = "tennisEmf")
  public class ParseUpcomingMatchesFile {
  
      @PersistenceContext(unitName = "tennisPersistenceUnit")
      EntityManager entityManager;
  
      @Autowired
      public MatchesRepository matchesRepository;
  
      @Autowired
      public PlayersRepository playersRepository;
  
      static final org.apache.log4j.Logger logger = Logger.getLogger(ParseUpcomingMatchesFile.class);
  
      public ParseUpcomingMatchesFile() {
      }
  
      public void parseMatchesFile(WebDriver webDriver, File file) throws IOException {
  
      String fullNameOne = "player1";
      String fullNameTwo = "player2";
      int matchId = matchesRepository.selectIdByPlayerNames(fullNameOne, fullNameTwo);
  
      }
  }
  </code></pre>
  
  <pre><code>@Configuration
  public class DatasourceConfig {
  
      @Autowired
      @Qualifier("tennisDataSource")
      private DataSource dataSource;
  
      @Autowired
      @Qualifier("tennisTm")
      PlatformTransactionManager transactionManager;
  
      public EntityManagerFactory setDataSource() {
  
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
          factoryBean.setDataSource(dataSource);
          factoryBean.setPersistenceUnitName("tennisPersistenceUnit");
          factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
          factoryBean.afterPropertiesSet();
  
          EntityManagerFactory factory = factoryBean.getNativeEntityManagerFactory();
          return factory;
      }
  }
  
  </code></pre>
  
  <pre><code>@Repository
  public interface MatchesRepository extends CrudRepository&lt;MatchesEntity, Integer&gt; {   
  
      @Query(value = "select m from MatchesEntity m")
      List&lt;MatchesEntity&gt; selectAllMatches();
  
      @Query(value = "select m.id from MatchesEntity m where m.namePlayer1 = :namePlayer1 and " +
              "m.namePlayer2 = :namePlayer2")
      int selectIdByPlayerNames(String namePlayer1, String namePlayer2);
  
     }
  
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tennisExecutor': Unsatisfied dependency expressed through field 'parseUpcomingMatchesFile'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parseUpcomingMatchesFile': Unsatisfied dependency expressed through field 'matchesRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'matchesRepository': Cannot create inner bean '(inner bean)#366583f9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#366583f9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
      at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151)
      at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131)
      at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
      at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:171)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5225)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 44 more
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parseUpcomingMatchesFile': Unsatisfied dependency expressed through field 'matchesRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'matchesRepository': Cannot create inner bean '(inner bean)#366583f9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#366583f9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
      ... 66 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'matchesRepository': Cannot create inner bean '(inner bean)#366583f9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#366583f9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:361)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:131)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1681)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1433)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
      ... 79 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#366583f9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:314)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
      at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:662)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:479)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:346)
      ... 92 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:771)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:303)
  </code></pre>
  
  <p>I tried to delete the .m2/repository folder, added the dependency for spring-data-jpa, but no change. Also, I tried to configure the datasource in order to achieve something, but it did not work.
  Does anyone have any suggestion?</p>