第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Spring 中更新數(shù)據(jù)源 bean?

如何在 Spring 中更新數(shù)據(jù)源 bean?

蕭十郎 2023-05-24 15:55:54
我的目標是使用 Spring 創(chuàng)建一個 Web 服務(wù)器。它必須實現(xiàn)多租戶,如果您不使其動態(tài)化(添加、刪除、更改),它會很好用。是否可以在 Spring 中更新數(shù)據(jù)源 bean?我的代碼:@SpringBootApplicationpublic class MyApplication {    public static void main(String[] args) throws IOException {        SpringApplication.run(MyApplication.class, args);    }    //Multitenancy    @Bean    public DataSource dataSource(){        //implements AbstractRoutingDataSource        CustomRoutingDataSource customDataSource = new CustomRoutingDataSource();        //logic here        return customDataSource;    }}我試過的:CustomRoutingDataSource c = context.getBean(CustomRoutingDataSource.class);c.setTargetDataSources(CustomRoutingDataSource.getCustomDatasources());它更新 bean(?) 但不更新 Spring 的數(shù)據(jù)源,如果使用此方法添加,數(shù)據(jù)庫連接仍然丟失。
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經(jīng)驗 獲得超12個贊

對于有同樣問題的人的簡單解決方案:


添加@RefreshScope


    @Bean

    @RefreshScope

    public DataSource dataSource() {

        CustomRoutingDataSource customDataSource = new CustomRoutingDataSource();

        ...

        return customDataSource;

    }

在 pom.xml 中添加彈簧執(zhí)行器端點


<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

POST 到/actuator/refresh更新數(shù)據(jù)源!


查看完整回答
反對 回復(fù) 2023-05-24
  • 1 回答
  • 0 關(guān)注
  • 207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號