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

Spring Boot配置和使用兩個(gè)數(shù)據(jù)源的實(shí)現(xiàn)步驟

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

Spring Boot配置和使用兩個(gè)數(shù)據(jù)源

技術(shù)背景

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

實(shí)現(xiàn)步驟

1. 配置數(shù)據(jù)源信息

application.propertiesapplication.yml中添加兩個(gè)數(shù)據(jù)源的配置信息。以application.properties為例:

# 第一個(gè)數(shù)據(jù)庫配置
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver
# 第二個(gè)數(shù)據(jù)庫配置
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

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

在配置類中創(chuàng)建兩個(gè)數(shù)據(jù)源的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. 配置事務(wù)管理器(可選)

如果需要對(duì)兩個(gè)數(shù)據(jù)源進(jìn)行事務(wù)管理,可以配置兩個(gè)事務(wù)管理器:

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. 使用不同的數(shù)據(jù)源

在需要使用數(shù)據(jù)源的地方,通過@Qualifier注解指定使用哪個(gè)數(shù)據(jù)源:

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() {
        // 使用主數(shù)據(jù)源執(zhí)行操作
        primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
        // 使用從數(shù)據(jù)源執(zhí)行操作
        secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
    }
}

核心代碼

數(shù)據(jù)源配置

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();
    }
}

事務(wù)管理器配置

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;
    }
}

服務(wù)層使用數(shù)據(jù)源

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() {
        // 使用主數(shù)據(jù)源執(zhí)行操作
        primaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
        // 使用從數(shù)據(jù)源執(zhí)行操作
        secondaryJdbcTemplate.execute("SELECT 1 FROM DUAL");
    }
}

最佳實(shí)踐

  • 標(biāo)記主數(shù)據(jù)源:使用@Primary注解標(biāo)記一個(gè)主數(shù)據(jù)源,這樣在自動(dòng)注入時(shí)Spring會(huì)優(yōu)先使用該數(shù)據(jù)源。
  • 事務(wù)管理:對(duì)于需要同時(shí)操作兩個(gè)數(shù)據(jù)源的場景,使用ChainedTransactionManager進(jìn)行事務(wù)管理。
  • 代碼隔離:將不同數(shù)據(jù)源的配置和使用代碼進(jìn)行隔離,提高代碼的可維護(hù)性。

常見問題

1.jdbcUrl is required with driverClassName錯(cuò)誤

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

2. 如何讓不同的JpaRepository使用不同的數(shù)據(jù)源

可以通過配置不同的EntityManagerFactoryTransactionManager,并在@EnableJpaRepositories注解中指定對(duì)應(yīng)的entityManagerFactoryReftransactionManagerRef。

3. 分布式事務(wù)問題

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

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

相關(guān)文章

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

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

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

    Java封裝(Encapsulation)實(shí)踐

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

    Spring系列之事物管理

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

    JavaOOP封裝實(shí)例解讀

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

    Shiro安全框架的主要組件及認(rèn)證過程簡介

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

    SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)

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

    關(guān)于Springboot如何獲取IOC容器

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

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

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

    Springboot?內(nèi)部服務(wù)調(diào)用方式

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

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

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

最新評(píng)論

长沙县| 金坛市| 连山| 西青区| 水富县| 山西省| 阳原县| 海阳市| 正阳县| 新田县| 昌江| 乐至县| 常熟市| 交城县| 讷河市| 周至县| 渝中区| 灵川县| 福鼎市| 曲沃县| 湄潭县| 玉门市| 嘉鱼县| 公主岭市| 临西县| 六盘水市| 黑水县| 五常市| 惠安县| 拉萨市| 金坛市| 民权县| 莆田市| 海南省| 大理市| 石景山区| 潼南县| 千阳县| 香格里拉县| 邵武市| 登封市|