美文网首页
A component required a bean name

A component required a bean name

作者: WMSmile | 来源:发表于2020-05-26 20:46 被阅读0次

    在学习spring-boot2.3.0的过程中遇到了Spring-boot: required a bean named 'entityManagerFactory' that could not be foundA component required a bean named 'entityManagerFactory' that could not be found.

    解决办法:
    我收到错误消息,例如“找不到名为'entityManagerFactory'的bean”。经过大量的谷歌搜索解决了这个问题。我为JPA设置了手动配置。

    @Bean 
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
    
        return sessionFactory;
    }
    

    但是默认情况下,JPA通过名称'entityManagerFactory'搜索sessionFactory,因此将我的代码更改为:

    @Bean(name="entityManagerFactory")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    
        return sessionFactory;
    }
    

    可以手动设置 @Bean(name="entityManagerFactory") 也可以自动设置,就是方法名字一定要是entityManagerFactory

    另一种解法

    @Bean 
    public LocalSessionFactoryBean entityManagerFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
    
        return sessionFactory;
    }
    

    原文地址

    相关文章

      网友评论

          本文标题:A component required a bean name

          本文链接:https://www.haomeiwen.com/subject/zhcbahtx.html