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

相关推荐
Sarvartha6 小时前
final 关键字
java·开发语言
2601_952047796 小时前
单一策略深度教程:用轻易云把集成任务的报错实时推送到钉钉机器人
java·机器人·钉钉
Joy T6 小时前
Spring AI 项目中的 pom.xml、application.yml 与 Nacos:从依赖管理到运行配置
spring·nacos·pom.xml·profile·参数配置·application.yml
数据狐(Datafox)7 小时前
淘宝图片搜索 API 落地实战:基于以图搜货搭建跨境电商选品系统
java·大数据·微服务
Escalating_xu9 小时前
【C 语言】数据在内存中的存储:补码、大小端、整型陷阱与 IEEE 754 全解析
java·c语言·网络
灯澜忆梦9 小时前
【面向对象编程C++】| 基础语法
java·c++·算法
一个风轻云淡9 小时前
GCC 和 GDB命令简单解读
java·linux·前端
省钱兄--zs9 小时前
西安24小时自助健身房解决方案实战:系统开发与部署全流程指南
java·spring boot·系统架构·intellij-idea·需求分析