Код: Выделить всё
ConfigurationDataSourceКод: Выделить всё
import com.example.hikaricp_demo.domain.TenantConfig;
import com.example.hikaricp_demo.repository.TenantConfigRepository;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Configuration
public class ConfigurationDataSource {
@Autowired
private TenantConfigRepository repository;
private List getListTenant(){
return repository.findAll();
}
TenantConfig tenantDefault() {
return repository.findByTenantId("tenantDefault");
}
@Bean
public DataSource dataSource() {
Map resolvedDataSources = new HashMap();
List listTenant = getListTenant();
for (TenantConfig item : listTenant) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(item.getDriverClassName());
hikariConfig.setJdbcUrl(item.getUrl());
hikariConfig.setUsername(item.getUsername());
hikariConfig.setPassword(item.getPassword());
hikariConfig.setSchema(item.getSchema());
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
resolvedDataSources.put(item.getTenantId(), dataSource);
}
AbstractRoutingDataSource dataSource = new MultiTenantDataSource();
dataSource.setDefaultTargetDataSource(tenantDefault());
dataSource.setTargetDataSources(resolvedDataSources);
dataSource.afterPropertiesSet();
return dataSource;
}
}
Код: Выделить всё
Description:
The dependencies of some of the beans in the application context form a cycle:
entityManagerFactory defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]
┌─────┐
| dataSourceScriptDatabaseInitializer defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]
↑ ↓
| configurationDataSource (field private com.example.hikaricp_demo.repository.TenantConfigRepository com.example.hikaricp_demo.config.ConfigurationDataSource.repository)
↑ ↓
| tenantConfigRepository defined in com.example.hikaricp_demo.repository.TenantConfigRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑ ↓
| jpaSharedEM_entityManagerFactory
└─────┘
Компонент dataSource зависит от TenantConfigRepository (для получения конфигураций арендатора с помощьюrepository.findAll()) .
TenantConfigRepository также может зависеть от DataSource, создавая циклическую зависимость.
Подробнее здесь: https://stackoverflow.com/questions/792 ... t-form-a-c
Мобильная версия