当前位置: 首页 > 知识库问答 >
问题:

没有名为'entityManagerFactory'的bean可用

谭裕
2023-03-14

我创建了一个Spring Boot应用程序,并使用我想要执行的方法创建了一个类。将项目部署为war文件时,我从stacktrac中获取错误。我想从类TennisExecitor中运行该方法。

  • 没有名为'entityManagerFactory'的bean可用
  • 创建名为'(内部bean)#366583f9'的bean时出错:设置构造函数参数时无法解析对bean'entityManagerFactory'的引用;嵌套异常org.springframework.beans.factory.NoSuchBean定义异常:没有名为'entityManagerFactory'的bean可用

我将显示代码的某些部分:


@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));

     }

@Configuration
@EnableJpaRepositories(basePackageClasses = {ParseUpcomingMatchesFile.class, ParseFinishedMatchesFile.class,
        PlayersProcessor.class}, entityManagerFactoryRef = "tennisEmf", transactionManagerRef = "tennisTm")
@EnableTransactionManagement
class ApplicationConfig {

    @Bean
    public EntityManagerFactoryBuilder builder(Environment environment) {
        Map<String, String> jpaProperties = new HashMap<>();
        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);
    }
}
@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);

    }
}
@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;
    }
}

@Repository
public interface MatchesRepository extends CrudRepository<MatchesEntity, Integer> {   

    @Query(value = "select m from MatchesEntity m")
    List<MatchesEntity> selectAllMatches();

    @Query(value = "select m.id from MatchesEntity m where m.namePlayer1 = :namePlayer1 and " +
            "m.namePlayer2 = :namePlayer2")
    int selectIdByPlayerNames(String namePlayer1, String namePlayer2);

   }

堆栈跟踪:

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)

我试图删除。m2/repository文件夹,添加了对spring数据jpa的依赖,但没有更改。此外,我试图配置数据源以实现某些功能,但没有成功。有人有什么建议吗?

共有1个答案

狄新翰
2023-03-14

对于Spring Boot 2,您需要添加此依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
 类似资料:
  • 我正在尝试开发一个新的批与Spring批和jpa。当我尝试启动它时,我收到错误"嵌套异常是org.springframework.beans.factory.NoSuchBean定义异常:没有名为'entityManagerFactory'可用的bean" 我的pom.xml 我的数据源配置类: 我的存储库类: 我的实体类: pplication.properties: 如果我尝试启动应用程序,我

  • 我正在开发一个新的应用程序(对我来说),我在启动时收到了无bean“entityManagerFactory”可用错误。其他经历过这种情况的人表示,如果我正确定义了数据源,这种情况就不会发生。让我困惑的是,Spring Boot/JPA或Flyway(我也在使用)坚持将特定的属性名称用于您的一个数据源。我不知道如果你有一个以上的人会怎么处理。我需要更改我们的申请。属性文件,以便从环境变量中获取值,

  • 我想开发一个模块化的应用程序,使用MySql数据库,Java11和Spring启动。在我添加模块描述符之前,这个应用程序运行得非常好;所以我只能猜测我在module-info.java中丢失了一些东西,但我不知道它是什么。我感谢任何帮助。这是我的密码 波姆。xml 应用yml module-info.java 使用者JAVA UsersRepository.java 用户资源。JAVA 演示应用程

  • 我正在开发一个小应用程序,只是为了自己尝试Spring Boot。它有五个不同的模块(eclipse项目): 模型——包含实体类、DTO和映射器以在它们之间切换 数据--包含存储库 服务——包含服务及其实现 reserve management--包含预订控制器和spring应用程序本身 人员管理-包含人员数据的控制器和Spring应用程序本身 如果我启动这两个应用中的任何一个,它们都可以正常运行

  • 请求处理失败;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有名为“transactionManager”的bean可用:没有为限定符“transactionManager”找到匹配的transactionManager bean-既没有限定符匹配,也没有bean名称匹配! 客户请求。JAVA 客户控制器 顾客道 spri

  • 当我试图使用spring代码创建关系时,我得到了事务管理器错误。我正在我的项目中使用Mysql和Neo4j数据库。我尝试了不同的解决方法,但都解决不了。 noSuchBeanDefinitionException:没有名为“Transaction Manager”的bean可用:没有为限定符“Transaction Manager”找到匹配的PlatformTransactionManager b