我试图按照this链接在我的应用程序中配置两个数据库 .
在最近的 Spring 天,我们没有 org.springframework.boot.autoconfigure.jdbc.TomcatDataSourceConfiguration 课程 .
org.springframework.boot.autoconfigure.jdbc.TomcatDataSourceConfiguration
使用它的替代方法是什么 .
我正在使用gradle进行 Spring 季启动应用程序
我更喜欢使用“ org.apache.tomcat.jdbc.pool.DataSource ”,然后手动配置我的数据源 .
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
我设置了完全限定名称,因为您应该从您制作的任何工厂方法返回“javax.sql.DataSource” .
使用spring-boot自动配置获取多个数据源是一个平底锅,因为自以为是的观点是您应该为每个数据源创建不同的服务 .
这并不总是可行的,所以这就是我在单个应用程序中需要多个DataSource时所做的一切 .
Disable the auto configuration like so:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class YourApp{}
Create a configuration properties for your datasources:
属性文件:
the-first.datasource.url=<insert value> the-first.datasource.username=<insert value> the-first.datasource.pw=<insert value> the-first.datasource.min-idle=<insert value> the-first.datasource.max-idle=<insert value> the-first.datasource.max-active=<insert value> the-first.datasource.validation-query=SELECT 1 # etc ... the-second.datasource.url=<insert value> the-second.datasource.username=<insert value> the-second.datasource.pw=<insert value> the-second.datasource.min-idle=<insert value> the-second.datasource.max-idle=<insert value> the-second.datasource.max-active=<insert value> the-second.datasource.validation-query=SELECT 1
配置类:
import lombok.*; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Getter @Setter @NoArgsConstructor @AllArgsConstructor @Component("theFirstDataSourceProperties") @ConfigurationProperties("the-first.datasource") public class TheFirstDataSourceProperties{ @NonNull private String password; @NonNull private String url; @NonNull private String username; private int minIdle; private int maxIdle; private int maxActive; @NonNull private String driverClassName; @NonNull private String validationQuery; }
Add a data source configuration class:
确保将其中一个标记为“@Primary”以帮助注射 .
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * Copyright ${year} * * @author J. Keith Hoopes */ @Configuration public class ExampleOfMultipleDataSourceConfiguration{ @Bean(name = "theFirstDataSource") @Primary @Autowired public DataSource theFirstDataSource( TheFirstDataSourceProperties theFirstDataSourceProperties){ //Fully qualified to not conflict with generic DataSource org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); // General dataSource.setName("theFirstDataSourceName"); dataSource.setDriverClassName(theFirstDataSourceProperties.getDriverClassName()); // etc .... return dataSource; } @Bean(name = "bDataSource") @Autowired public DataSource theSecondDataSource( TheSecondDataSourceProperties theSecondDataSourceProperties){ //Fully qualified to not conflict with generic DataSource org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); // General dataSource.setName("theSecondDataSourceName"); dataSource.setDriverClassName(theSecondDataSourceProperties.getDriverClassName()); // etc .... return dataSource; } }
Inject your custom DataSources where needed using @Qualifier so you get the correct one:)
@Qualifier("theFirstDataSource")
利润?是 .
Oh, and here are the basic dependencies I use.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-metadata</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
1 回答
我更喜欢使用“ org.apache.tomcat.jdbc.pool.DataSource ”,然后手动配置我的数据源 .
我设置了完全限定名称,因为您应该从您制作的任何工厂方法返回“javax.sql.DataSource” .
使用spring-boot自动配置获取多个数据源是一个平底锅,因为自以为是的观点是您应该为每个数据源创建不同的服务 .
这并不总是可行的,所以这就是我在单个应用程序中需要多个DataSource时所做的一切 .
Disable the auto configuration like so:
Create a configuration properties for your datasources:
属性文件:
配置类:
Add a data source configuration class:
确保将其中一个标记为“@Primary”以帮助注射 .
Inject your custom DataSources where needed using @Qualifier so you get the correct one:)
利润?是 .
Oh, and here are the basic dependencies I use.