我正试图在春季启动应用程序中实现多租户。我指的是"http://anakiou.blogspot.com/2015/08/multi-tenant-application-with-spring.html"示例。在这个示例中,一切都很好。但在我的代码中,数据源之间的切换没有发生。
在我的DataSourceConfig.java中,我面临着声明3个Beans的问题,所以我使用了@Primary注释。如果我删除@Primary注释,我将收到以下错误
' Parameter 1 of method liquibase in com.config.DatabaseConfiguration required a single bean, but 3 were found:
- dataSource: defined by method 'dataSource1' in class path resource [com/config/DataSourceConfig.class]
- dataSource2: defined by method 'dataSource2' in class path resource [com/config/DataSourceConfig.class]
- dataSource3: defined by method 'dataSource3' in class path resource [com/config/DataSourceConfig.class] '
但是,如果我使用@Primary注释而不考虑租户id,那么它将把数据存储在用@Primary进行注释的DataSource中。因此,数据源的切换并没有发生。
这是我的DataSourceConfig类
package com.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.netflix.governator.annotations.binding.Secondary;
@Configuration
public class DataSourceConfig {
@Autowired
private MultitenancyProperties multitenancyProperties;
@Primary
@Bean(name = {"dataSource" , "dataSource1"})
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource1().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource1().getDriverClassName())
.username(this.multitenancyProperties.getDatasource1().getUsername())
.password(this.multitenancyProperties.getDatasource1().getPassword())
.url(this.multitenancyProperties.getDatasource1().getUrl());
return factory.build();
}
@Bean(name = {"dataSource2"})
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2(@Qualifier("dataSource1") DataSource dataSource1) {
System.out.println("Entered to 2");
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource2().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource2().getDriverClassName())
.username(this.multitenancyProperties.getDatasource2().getUsername())
.password(this.multitenancyProperties.getDatasource2().getPassword())
.url(this.multitenancyProperties.getDatasource2().getUrl());
return factory.build();
}
@Bean(name = {"dataSource3"})
@ConfigurationProperties(prefix = "spring.datasource.datasource3")
public DataSource dataSource3() {
DataSourceBuilder factory = DataSourceBuilder
.create(this.multitenancyProperties.getDatasource3().getClassLoader())
//.driverClassName(this.multitenancyProperties.getDatasource3().getDriverClassName())
.username(this.multitenancyProperties.getDatasource3().getUsername())
.password(this.multitenancyProperties.getDatasource3().getPassword())
.url(this.multitenancyProperties.getDatasource3().getUrl());
return factory.build();
}}
禁用Spring的DataSourceAutoConfiguration并提供我们的多租户DataSourceConfig。这将从MultitenancyProperties加载属性,而MultitenanceProperties又由application.properties配置,并相应地配置我们的数据源。要排除DataSourceAutoConfiguration,请使用@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})