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

相关推荐
SL_staff37 分钟前
3天上线OKR系统:一名HR与1名工程师如何用JVS完成全栈交付
java·低代码·开源
2501_933923251 小时前
统一功能:统一返回格式+统一异常处理
spring·java-ee·状态模式·统一数据返回
wzdark2 小时前
基于链表的内存池设计与内存复用机制4
java·数据结构·链表
cnkeysky2 小时前
idea 中的 maven 项目重新加载子模块
java·idea
Escalating_xu2 小时前
【System V 信号量】从 P/V 原语到 Builder 封装:写出可控、可清理的进程互斥组件
java·linux·开发语言·jvm
今天AI了吗2 小时前
什么是 AI Agent?它与直接调用大模型 API 有何区别
java·网络·人工智能·架构·java-ee
步行cgn2 小时前
Spring 注入 Map 集合详解
数据库·python·spring
隐退山林3 小时前
JavaEE进阶:SpringAOP
java·java-ee
Wang's Blog3 小时前
Java框架快速入门: Spring Security+OAuth2之方法级安全注解
java·安全·spring
JavacKaka3 小时前
LiteFlow规则引擎实战博客-2026-09-09
java