spring boot3报错Cannot resolve reference to bean ‘jpaSharedEM_entityManagerFactory‘ while setting bean

项目更新成spring boot3.1.5后启动一直报错

Exception encountered during context initialization - 
cancelling refresh attempt: org.springframework.beans.factory.
UnsatisfiedDependencyException: 
Error creating bean with name 'shiroConfiguration': 
Unsatisfied dependency expressed through 
field 'userRepository': Error creating 
bean with name 'userRepository' defined in com.xxx.xxxx.widget.user.
repository.
UserRepository defined in @EnableJpaRepositories 
declared on UserApplication: Cannot resolve 
reference to bean 'jpaSharedEM_entityManagerFactory' 
while setting bean property 'entityManager'

研究了很多配置文件,都无果
最后发现引入jpa最新的jar包后就自动解决了

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>6.3.1.Final</version>
</dependency>

浪费了大概有一天半 的时间解决这个问题。
重点还有 需要引入的其它配置

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

spring.main.allow-bean-definition-overriding= true

附jpa管理配置

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",//配置连接工厂 entityManagerFactory
        transactionManagerRef="transactionManagerPrimary", //配置 事物管理器  transactionManager
        basePackages = {"com.xxx.xxxx.xxxx",
                "com.xxx.xx.xxxx.xxx.*",})//设置Repository所在位置
public class MasterDataSourceConfig {

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactory(builder).getObject().createEntityManager();
    }

    @Resource
    private JpaProperties jpaProperties;

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .properties(jpaProperties.getProperties())
                .packages("com.XXXXXX.xxxx.xxxx",
                        "com.XXXXXX.xxxx.xxx.xxx"
                ) //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactory(builder).getObject());
    }

}

你可能感兴趣的:(spring,boot,后端,java,jpa)