Requested bean is currently in creation: Is there an unresolvable circular reference?

Requested bean is currently in creation: Is there an unresolvable circular reference?

Spring bootを2.5.12にバージョンアップしたら「Requested bean is currently in creation: Is there an unresolvable circular reference?」エラーが発生しました。

bitronixを使用して複数データベースに接続していたのですが、その箇所でエラーとなっていたので修正しました。DBはPostgreSQLです。

package com.example.demo.config;

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.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import bitronix.tm.resource.jdbc.PoolingDataSource;

@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(entityManagerFactoryRef = "db1EntityManagerFactory", transactionManagerRef = "transactionManager", basePackages = "com.example.demo.repository.primary")
public class PrimaryDbConfig {

  @Autowired private MyDataSource1 dataSource;
// @Autowired private JpaVendorAdapter jpaVendorAdapter; // コメントアウト

  @Primary
  @Bean(name = "db1DataSource")
  public DataSource dataSource() {
    final PoolingDataSource xaDataSource = dataSource.getXAPrimaryDataSource();
    xaDataSource.setUniqueName("db1DataSource");
    return xaDataSource;
  }

  @Primary
  @Bean(name = "db1EntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
    @Qualifier("db1DataSource") DataSource dataSource) {
    final Map<String, Object> properties = new HashMap<>();
    properties.put("hibernate.transaction.jta.platform", BJtaPlatform.class.getName());
    properties.put("javax.persistence.transactionType", "JTA");
    properties.put("hibernate.jdbc.lob.non_contextual_creation", true);
    properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL10Dialect"); // 追加
    final LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
    entityManager.setJtaDataSource(dataSource);
    final JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); // 追加
    entityManager.setJpaVendorAdapter(jpaVendorAdapter); // 追加
    entityManager.setPackagesToScan("com.example.demo.entity.primary");
    entityManager.setPersistenceUnitName("PrimaryDataBase");
    entityManager.setJpaPropertyMap(properties);
    return entityManager;
  }
}

Spring bootのバージョンアップって何気に面倒です。

Spring Boot+JPA+bitronixで2相コミットを実装する方法

Spring Boot 2.6 Release Notes
Spring Boot. Contribute to spring-projects/spring-boot development by creating an account on GitHub.
How to set some Hibernate properties in Spring JPA Web Application?
I am trying to get rid of the typical persistence.xml file in Spring JPA web application. So far, I have managed to inje...

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました