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代码审查还是挺有用的

相关推荐
SunnyDays10111 小时前
Java 如何为 PDF 添加、修改和删除书签
java·pdf
铃木之影2 小时前
Java 版本 RAG 示例(Spring AI + Milvus)
java·人工智能·spring
C++、Java和Python的菜鸟2 小时前
第14章 项目部署(Linux)
java
智海深蓝3 小时前
智慧渔业海上养殖数字孪生实践方向与难点拆解分析
java·前端·网络
2601_955760073 小时前
Claude API 多人协作中的版本管理方法
java·ai编程
梦想的旅途23 小时前
企业微信API二次开发:外部群模块功能清单与全场景对接
java·python·企业微信
校招VIP3 小时前
[校大]27届东华理工大学JAVA简历:中厂简历通过率30%
java·秋招·校招·实习·产品岗·27届
老白干4 小时前
基于 Spring AOP 的操作日志记录:以 DeptController 增删接口为例
java·python·spring
晚安code4 小时前
Java编程规范避坑指南:阿里开发手册15条强制规约实战解析
java·后端