Spring Boot 说没有可用的名为“entityManagerFactory”的bean
Posted
技术标签:
【中文标题】Spring Boot 说没有可用的名为“entityManagerFactory”的bean【英文标题】:Spring Boot says no bean named 'entityManagerFactory' available 【发布时间】:2021-07-08 02:52:38 【问题描述】:我正在开发一个新应用程序(对我来说),我在启动时收到 no bean 'entityManagerFactory' available 错误。其他人认为如果我正确定义了数据源,就不会发生这种情况。让我感到困惑的是,Spring Boot/JPA 或 Flyway(我也在使用)坚持将特定属性名称用于您的 one 数据源。如果你有多个,我不确定有人会如何处理它。我需要更改我们的 application.properties 文件以从环境变量中获取,以便他们可以从秘密中获取值。我有一个定义,以便 flyway 可以进行可能的迁移。我有另一个这样 Spring-Boot 可以完成基于 JPA 的工作。我有另一个,因为现有代码之前定义了一个 dataSource 并相应地进行了连接。所有人都将进入同一个数据库。我希望我只能使用一组 application.properties 属性,但更重要的是,我希望解决这个 entityManagerFactory 错误。因为我的修复感觉像是一团糟,所以我想伸出手来看看我不理解的地方。
这是 application.properties
spring.profiles.active=sprint-vault-services-not-available
spring.application.name=file-generator
file.generator.schema=FILE_GENERATOR
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration
spring.flyway.schemas=$file.generator.schema
spring.flyway.baseline-on-migrate=false
file-generator.date-format=MMddyy
file-generator.time-format=HHmmss
file-generator.ebcdic-output-path=app/output/ebcdic
file-generator.csv-output-path=app/output/csv
file-generator.header-timestamp-format=yyyyMMddHHmmss
file-generator.file-sequence-number=1
logging.path=app/logs
bcupload_datasource_url=$BCUPLOAD_DATASOURCE_URL
#spring.jpa.properties.hibernate.default_schema=FILE_GENERATOR
bcupload_datasource_username=$BCUPLOAD_DATASOURCE_USERNAME
bcupload_datasource_password=$BCUPLOAD_DATASOURCE_PASSWORD
bcupload_datasource_driver=$BCUPLOAD_DATASOURCE_DRIVER
bcupload_datasource_flyway_db_name=LocalFileGenerator
spring.datasource.driver-class-name=$BCUPLOAD_DATASOURCE_DRIVER
spring.datasource.url=$BCUPLOAD_DATASOURCE_URL
spring.datasource.password=$BCUPLOAD_DATASOURCE_PASSWORD
spring.datasource.username=$BCUPLOAD_DATASOURCE_USERNAME
spring.jpa.database-platform=DB2Platform
spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.flyway.url= $BCUPLOAD_DATASOURCE_URL
spring.flyway.user=$BCUPLOAD_DATASOURCE_USERNAME
spring.flyway.password=$BCUPLOAD_DATASOURCE_PASSWORD
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
2021-04-12 23:06:34 DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:814)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1282)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:691)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:508)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:374)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1699)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1444)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:623)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:611)
at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:51)
at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:36)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:897)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.mycompany.cloud.cost.ssc.file.generator.FileGeneratorApplication.main(FileGeneratorApplication.java:22)
数据源的Bean定义:
package com.ibm.cio.cloud.cost.ssc.file.generator.configuration;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@Configuration
public class DataSourceConfig
private String dataSourceUrl;
private String userName;
private String password;
private String driver;
/*
@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);
*/
@Autowired
public DataSourceConfig(@Value("$bcupload_datasource_url") String bcupload_datasource_url,
@Value("$bcupload_datasource_username") String bcupload_datasource_username,
@Value("$bcupload_datasource_password") String bcupload_datasource_password,
@Value("$bcupload_datasource_driver") String bcupload_datasource_driver)
this.dataSourceUrl = bcupload_datasource_url;
this.userName = bcupload_datasource_username;
this.password = bcupload_datasource_password;
this.driver = bcupload_datasource_driver;
@Bean(name = "bcUploadDataSource")
public DataSource dataSource()
return DataSourceBuilder.create()
.url(dataSourceUrl)
.username(userName)
.password(password)
.driverClassName(driver).build();
【问题讨论】:
我认为,由于您的 bean 名称bcUploadDataSource
与 Spring Boot 可能预期的不同,它无法创建其他 bean 以供数据库工作,您能否查看日志以获取更多警告和错误消息?
【参考方案1】:
我有一个包含多个数据源的项目,每个数据源最终看起来像这样:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
@EnableJpaRepositories(
basePackages = "com.your.repositories.packages",
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager")
public class DataSourceConfig
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManager()
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] "com.entity.pacakges");
em.setPersistenceUnitName("your_name");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("hibernate.show-sql", env.getProperty("spring.jpa.show-sql"));
properties.put("hibernate.temp.use_jdbc_metadata_defaults",
env.getProperty("spring.jpa.use_jdbc_metadata_defaults." + NAME));
properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
properties.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
em.setJpaPropertyMap(properties);
return em;
@Bean(name = "dataSource")
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager()
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManager().getObject());
return transactionManager;
所以对于您的错误,可能尝试使用 LocalContainerEntityManagerFactoryBean 而不是 EntityManagerFactoryBuilder
【讨论】:
env.getProperty("spring.jpa.use_jdbc_metadata_defaults."+NAME. NAME 是数据库名称吗?我认为您提到每个数据源有多个 DataSourceConfig,但我不完全理解如何这将得到实施。顺便说一句,感谢您的努力。我非常感谢您花时间制作响应。 嘿,是的,抱歉 NAME 只是一个变量,我为了简化答案而删除了,但忘记在此处删除它,在我们的例子中,它只是一个名为“PROD”的字符串。 所以是的,基本上你为你拥有的每个数据源创建一个类似的类。然后,根据您为 @EnableJpaRepositories 注释提供的参数,此包中的存储库将自动使用此数据源。让我知道这对你是否有意义。 假设我有两个数据源,都有一个 JDBC URL、一个用户名字符串、一个密码字符串,都在一个系统环境变量中。我会在上面的 dataSource() 方法中指定传递这些 env 值吗?我过去因为没有 spring.datasource 属性而被烧毁。我不确定如何告诉 Spring-Boot/JPA/Hikari 我不只有一个数据源,所以不要查看 spring.datasource。让我告诉你去哪里看。以上是关于Spring Boot 说没有可用的名为“entityManagerFactory”的bean的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot(带有 jpa 的 mysql):没有名为“entityManagerFactory”的 bean 可用
没有可用的“javax.sql.DataSource”类型的合格bean - spring-boot-batch
在 WebFlux 中创建名为 requestMappingHandlerMapping 的 bean 时出错(没有 Spring Boot)
Spring boot + Hibernate + JPA 没有可用的事务性 EntityManager