解决A component required a bean named 'entityManagerFactory' that could not be found.问题

问题

在调试SpringBoot集成多数据源的工程过程中,启动异常。错误如下:

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

 

分析

统计了一下网上针对此类问题发生的原因:

【原因1】jar包冲突
https://blog.csdn.net/babyyaoyao/article/details/81132016
        该文是因为在pom.xml文件中spring-boot-starter-data-jpa中指定了2.0.3.RELEASE导致,解决方案是删除该version即可。

        
            org.springframework.boot
            spring-boot-starter-data-jpa
            2.0.3.RELEASE
        

        

其解释发生异常的原因可能是出现了与hibernate的jar包冲突。
        
【原因2】jar包损坏
1)helsonxiao commented on 28 Mar 2018
I had the same error. After deleting all repos under .m2 folder and redownloading. It works.

2)HSamiSari answered Jul 12  2018

I had exact same issue. When I checked maven build log I have realised that there was an error about hibernate packages complaining about "invalid LOC header (bad signature)". I solved by deleting sub directories under .m2\repository\org\hibernate\hibernate-core and recompiling my project.
    
解决方式是删除损坏的jar,重新加载。

 

以上两个解决方式我都试过,但问题依然未解决。说明我的问题是另外一种情况。

后面再仔细研究代码,尝试调整了一下代码,终于找到原因:

在DBConfig1.java文件中错误的配置数据源的方式:

    …… 

    private DataSource dataSource1;

	/**
	 * @methodDesc: 功能描述:(配置数据源)
	 */
	@Bean(name = "dataSource1")
	@ConfigurationProperties(prefix = "mysql.datasource.test1")
	public DataSource createDataSource() {
		dataSource1 = DataSourceBuilder.create().build();
		return dataSource1;
	}

	……

	@Primary
	@Bean(name = "entityManagerFactory1")
	public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
		if(dataSource1 == null) {
			createDataSource();
		}
		return builder.dataSource(dataSource1).properties(getVendorProperties())
				.packages("com.example.demo.entity") // 设置实体类所在位置
				.persistenceUnit("primaryPersistenceUnit").build();
	}

 

解决

把数据源注入独立到一个配置类DataSourceConfig.java中:

    @Bean(name = "dataSource1")
    @Qualifier("dataSource1")
    @Primary
    @ConfigurationProperties(prefix="mysql.datasource.test1")
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

在另一个配置文件DBConfig1.java中使用该数据源:

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

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

	@Primary
	@Bean(name = "entityManagerFactory1")
	public LocalContainerEntityManagerFactoryBean       entityManagerFactory(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dataSource1).properties(getVendorProperties())
				.packages("com.example.demo.entity") // 设置实体类所在位置
				.persistenceUnit("primaryPersistenceUnit").build();
	}

问题解决了。

文章结束。

你可能感兴趣的:(SpringBoot)