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

相关推荐
l1t8 小时前
DeepSeek总结的DuckDB 如何更快地运行递归 CTE
java·开发语言·数据库·mysql·duckdb
小手cool8 小时前
使用递归对数组进行反转操作
java·数据结构·算法
MetaLite8 小时前
SpringBoot分页接口怎么设计-pageSize不设上限会发生什么
java·spring boot·后端
晚安code8 小时前
LangChain4j AiService 实战:会话记忆与结构化输出
java·langchain
晚安code8 小时前
LangChain4j 实战:RAG、工具调用、护轨与 SSE 流式输出
java·langchain
郑州光合科技余经理9 小时前
本地生活平台搭建:统一订单表与多后台切换怎么拆
java·开发语言·前端·系统架构·uni-app·php·ai编程
让你三行代码QAQ9 小时前
SpringAI-Advisors
spring·ai编程
杰哥哥不是个好叔叔9 小时前
【Spring AI】Spring AI 2.0.1 新特性袭来
java·人工智能·spring
莫陌尛.9 小时前
Java核心知识点:直接内存、CPU密集型线程池、并行流对比文档
java·开发语言
Zane199410 小时前
自定义异常该继承 Exception 还是 RuntimeException,就看这一个问题
java·后端