Spring-Configur注解

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

代码预警-内存泄漏


一、两份代码

重点在 public DataSource routingDataSource() 这个方法

会导致连接池多份配置:

java 复制代码
package com.atguigu.llmcallback.Configuration;

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 org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean
    public DataSource routingDataSource() {
        RoutingDataSource routing = new RoutingDataSource();

        Map<Object, Object> dsMap = new HashMap<>();
        dsMap.put("master", masterDataSource());
        dsMap.put("slave", slaveDataSource());

        routing.setDefaultTargetDataSource(masterDataSource());
        routing.setTargetDataSources(dsMap);

        return routing;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource routingDataSource) {
        return new JdbcTemplate(routingDataSource);
    }
    @Bean
    public PlatformTransactionManager transactionManager(DataSource routingDataSource) {
        return new DataSourceTransactionManager(routingDataSource);
    }


}

修改后:

java 复制代码
package com.atguigu.llmcallback.Configuration;

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 org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * ✅ 路由数据源(核心)
     */
    @Primary
    @Bean
    public DataSource routingDataSource(
            DataSource masterDataSource,
            DataSource slaveDataSource) {

        RoutingDataSource routing = new RoutingDataSource();

        Map<Object, Object> dsMap = new HashMap<>();
        dsMap.put("master", masterDataSource);
        dsMap.put("slave", slaveDataSource);

        routing.setDefaultTargetDataSource(masterDataSource);
        routing.setTargetDataSources(dsMap);

        return routing;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource routingDataSource) {
        return new JdbcTemplate(routingDataSource);
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource routingDataSource) {
        return new DataSourceTransactionManager(routingDataSource);
    }
}

二、原因

原来通过静态调用相当于直接new了一份,所以连接池会有多份一样的配置。

java 复制代码
@Bean
public DataSource routingDataSource() {
    DataSource master = masterDataSource(); // 普通方法调用
    DataSource slave = slaveDataSource();  // 普通方法调用
    ...
}
java 复制代码
routingDataSource() {
    DataSource master = new HikariDataSource(...); // new 了一个新的
    DataSource slave = new HikariDataSource(...);  // 又 new 了一个新的
}

Configuration注解会自动拦截配置参数,并在已有Bean实例中获取,所以可以保证单例。


总结

AI代码审查还是挺有用的

相关推荐
MetaLite13 分钟前
Spring Boot 加了 @Valid,嵌套参数为什么还没校验?
java·spring boot·后端
半杯咖啡半行码14 分钟前
QML与 Qt Quick入门实战:从零基础到 C++ 双向通信详解
java·qt·交互
霸道流氓气质21 分钟前
Lingua 语言检测库:n-gram 统计模型、置信度阈值调优与 Spring Boot 分层检测管道的生产级实战
java·spring boot·后端
霸道流氓气质26 分钟前
智能表单生成器:结构化输出契约、JSON Schema 驱动渲染与 EAV/JSONB 混合存储的 Vue3 + Spring AI 生产级实战
人工智能·spring·json
文丑颜不良啊31 分钟前
LeetCode1281~1300题目解析
java·leetcode
missile11135 分钟前
# HDR 算法 ISP 通路客制化方案(G100 / mt6789)
java·前端·javascript
全栈陈序员1 小时前
【Python】基础语法入门(三十三)—— 日志系统详解:用 logging 记录程序运行
java·开发语言·python
她的男孩1 小时前
低代码加字段后表结构怎么同步:预览安全 DDL,高风险变更再强制执行
java·spring boot·低代码
简简单单就是我_hehe1 小时前
一次 ClickHouse 服务器磁盘告警的排查与修复实录
java·服务器·前端
Thomas.Sir1 小时前
第11课:文件上传下载、跨域CORS配置,SpringBoot4.x跨域新方案
java·spring boot