最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot配置和使用兩個數據源的實現步驟

 更新時間:2025年07月22日 11:02:50   作者:1010n111  
本文詳解Spring Boot配置雙數據源方法,包含配置文件設置、Bean創(chuàng)建、事務管理器配置及@Qualifier注解使用,強調主數據源標記、代碼隔離和ChainedTransactionManager事務管理,對Spring Boot使用兩個數據源的相關知識感興趣的朋友一起看看吧

Spring Boot配置和使用兩個數據源

技術背景

在實際的開發(fā)場景中,一個Spring Boot應用可能需要連接多個數據庫,比如主從數據庫、不同業(yè)務模塊使用不同數據庫等。Spring Boot本身支持多數據源的配置,通過合理配置可以實現對多個數據源的管理和使用。

實現步驟

1. 配置數據源信息

application.propertiesapplication.yml中添加兩個數據源的配置信息。以application.properties為例:

# 第一個數據庫配置
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
# 第二個數據庫配置
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

2. 創(chuàng)建數據源Bean

在配置類中創(chuàng)建兩個數據源的Bean:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties(prefix="spring.secondDatasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

3. 配置事務管理器(可選)

如果需要對兩個數據源進行事務管理,可以配置兩個事務管理器:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class TransactionManagerConfig {
    @Bean(name="tm1")
    @Autowired
    @Primary
    DataSourceTransactionManager tm1(@Qualifier ("primaryDataSource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
        return txm;
    }
    @Bean(name="tm2")
    @Autowired
    DataSourceTransactionManager tm2(@Qualifier ("secondaryDataSource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
        return txm;
    }
}

4. 使用不同的數據源

在需要使用數據源的地方,通過@Qualifier注解指定使用哪個數據源:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class MyService {
    private final JdbcTemplate primaryJdbcTemplate;
    private final JdbcTemplate secondaryJdbcTemplate;
    @Autowired
    public MyService(@Qualifier("primaryDataSource") DataSource primaryDataSource,
                     @Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
        this.primaryJdbcTemplate = new JdbcTemplate(primaryDataSource);
        this.secondaryJdbcTemplate = new JdbcTemplate(secondaryDataSource);
    }
    public void doSomething() {
        // 使用主數據源執(zhí)行操作
        primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
        // 使用從數據源執(zhí)行操作
        secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
    }
}

核心代碼

數據源配置

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties(prefix="spring.secondDatasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

事務管理器配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class TransactionManagerConfig {
    @Bean(name="tm1")
    @Autowired
    @Primary
    DataSourceTransactionManager tm1(@Qualifier ("primaryDataSource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
        return txm;
    }
    @Bean(name="tm2")
    @Autowired
    DataSourceTransactionManager tm2(@Qualifier ("secondaryDataSource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
        return txm;
    }
}

服務層使用數據源

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class MyService {
    private final JdbcTemplate primaryJdbcTemplate;
    private final JdbcTemplate secondaryJdbcTemplate;
    @Autowired
    public MyService(@Qualifier("primaryDataSource") DataSource primaryDataSource,
                     @Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
        this.primaryJdbcTemplate = new JdbcTemplate(primaryDataSource);
        this.secondaryJdbcTemplate = new JdbcTemplate(secondaryDataSource);
    }
    public void doSomething() {
        // 使用主數據源執(zhí)行操作
        primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
        // 使用從數據源執(zhí)行操作
        secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
    }
}

最佳實踐

  • 標記主數據源:使用@Primary注解標記一個主數據源,這樣在自動注入時Spring會優(yōu)先使用該數據源。
  • 事務管理:對于需要同時操作兩個數據源的場景,使用ChainedTransactionManager進行事務管理。
  • 代碼隔離:將不同數據源的配置和使用代碼進行隔離,提高代碼的可維護性。

常見問題

1.jdbcUrl is required with driverClassName錯誤

在Spring Boot 2.0之后,需要使用jdbc-url代替url。

2. 如何讓不同的JpaRepository使用不同的數據源

可以通過配置不同的EntityManagerFactoryTransactionManager,并在@EnableJpaRepositories注解中指定對應的entityManagerFactoryReftransactionManagerRef。

3. 分布式事務問題

如果需要在兩個數據源之間進行分布式事務處理,可以考慮使用XA協(xié)議或分布式事務框架,如Atomikos。

到此這篇關于Spring Boot配置和使用兩個數據源的實現步驟的文章就介紹到這了,更多相關Spring Boot內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解SpringBoot 快速整合Mybatis(去XML化+注解進階)

    詳解SpringBoot 快速整合Mybatis(去XML化+注解進階)

    本篇文章主要介紹了詳解SpringBoot 快速整合Mybatis(去XML化+注解進階),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Java封裝(Encapsulation)實踐

    Java封裝(Encapsulation)實踐

    封裝是OOP核心原則,將數據和方法整合為類,通過訪問修飾符(如private)隱藏內部狀態(tài),僅提供受控的getter/setter訪問,確保數據安全、完整性,提升代碼可維護性,但可能增加代碼量與復雜度
    2025-09-09
  • Spring系列之事物管理

    Spring系列之事物管理

    這篇文章主要介紹了Spring系列之事物管理,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring方面知識具有一定的參考學習價值,需要的朋友們一起學習學習吧
    2021-09-09
  • JavaOOP封裝實例解讀

    JavaOOP封裝實例解讀

    封裝通過private限制屬性訪問,提供get/set方法控制數據讀寫,確保值合法,示例中Student類屬性私有,Test類需調用set方法賦值并驗證,get方法獲取值,實現數據隱藏與安全操作
    2025-09-09
  • Shiro安全框架的主要組件及認證過程簡介

    Shiro安全框架的主要組件及認證過程簡介

    這篇文章主要介紹了Shiro安全框架的主要組件及認證過程簡介,Shiro?是一個強大靈活的開源安全框架,可以完全處理身份驗證、授權、加密和會話管理,本文就來介紹一下此框架的核心組成,需要的朋友可以參考下
    2023-08-08
  • SpringBoot+ruoyi框架文件上傳和下載的實現

    SpringBoot+ruoyi框架文件上傳和下載的實現

    文件的上傳和下載功能,是項目開發(fā)過程中比較常見的業(yè)務需求,本文主要介紹了SpringBoot+ruoyi框架文件上傳和文件下載的實現,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 關于Springboot如何獲取IOC容器

    關于Springboot如何獲取IOC容器

    大家好,我是孤焰。最近我在制作日志審計功能時發(fā)現不知道怎樣獲取到Springboot項目中的IOC容器,經過摸索,最終解決了這個問題,現在把解決方式和大家分享一下
    2021-08-08
  • Java創(chuàng)建線程的五種寫法總結

    Java創(chuàng)建線程的五種寫法總結

    本文主要為大家詳細介紹一下Java實現線程創(chuàng)建的五種寫法,文中的示例代碼講解詳細,對我們學習有一定的幫助,感興趣的可以跟隨小編學習一下
    2022-08-08
  • Springboot?內部服務調用方式

    Springboot?內部服務調用方式

    這篇文章主要介紹了Springboot?內部服務調用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • JDK9對String字符串的新一輪優(yōu)化

    JDK9對String字符串的新一輪優(yōu)化

    這篇文章主要介紹了JDK9對String字符串的新一輪優(yōu)化,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03

最新評論

盘山县| 蒙山县| 若羌县| 玛沁县| 阿拉善左旗| 台湾省| 长汀县| 饶平县| 永兴县| 湘乡市| 香格里拉县| 儋州市| 陵水| 吉林省| 承德县| 庆安县| 鲜城| 辛集市| 山西省| 资中县| 合山市| 吴旗县| 名山县| 开化县| 马龙县| 翁牛特旗| 八宿县| 湖北省| 开封县| 溧水县| 吕梁市| 特克斯县| 昭觉县| 嘉兴市| 虞城县| 阜南县| 望奎县| 禄丰县| 招远市| 崇左市| 梓潼县|