如何在 spring 中拥有一个不是 bean 的数据源对象? [英] how can I have a datasource object that is not a bean in spring?

查看:30
本文介绍了如何在 spring 中拥有一个不是 bean 的数据源对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能很奇怪.但我想知道如何/是否可以在运行时而不是在容器启动时创建数据源对象.

This might sound weird. But I want to know how/if I can create datasource objects during runtime and not when the container start-up.

这是我正在解决的问题:

Here is the problem I am working on:

我有一个 MySql 数据库,其中存储了我需要连接并进行夜间处理的其他 SQL Server 的 URL、用户名和密码.此 SQL Server 列表每次都会更改.所以它不能硬编码在属性文件中.此外,SQL 服务器的数量约为 5000 或更多.

I have a MySql database which stores the URL, userName and password for other SQL Servers that I need to connect and do overnight processing. This list of SQL Servers changes everytime. so it cannot be hard-coded in properties files. Further, the no of SQL servers is about 5000 or more.

业务逻辑涉及读取 MySQL 数据库(当前是在容器启动期间创建的数据源 bean),对于 MySQL 数据库中 SQL_SERVER_MAPPING 表中的每个条目,我需要连接到该数据库并运行报告.

The business logic involves reading the MySQL database (which is currently a datasource bean created during container start-up) and for each entry in SQL_SERVER_MAPPING table in MySQL database, I need to connect to that database and run reports.

我正在考虑为每个 SQL 服务器实例做一些类似的事情

I was thinking of doing something along this line for each of the SQL server instances

public DataSource getdataSource(String url, String u, String p, String class) {
    return DataSourceBuilder
        .create()
        .username(u)
        .password(p)
        .url(url)
        .driverClassName(class)
        .build();
}

public JdbcTemplate jdbcTemplate(DataSource datasource) { 
        return new JdbcTemplate(dataSource); 
    } 

这是一个生成器,它为给定的 url 生成数据源并从中创建必要的 jdbcTemplate.所以基本上为每个 SQL 服务器配置创建一个.我担心的是我将创建大约 5000 个数据源和 5000 个 jdbcTemplate 甚至更多.这对我来说听起来不对.游览这里的正确方式是什么?

Here is a builder that generates datasource for a given url and create the necessary jdbcTemplate from it. so basically create one for each of SQL server configurations. My concern is I will be creating about 5000 datasources and 5000 jdbcTemplate or perhaps even more. That doesn't sound right to me. what is the right way to get around here?

有没有办法在我完成后立即删除数据源对象或回收它们?

is there a way to remove datasource objects as soon I am done with it or recycle them?

我是否应该在我的 Spring 应用程序中缓存这些 dataSource 对象,这样我就不必每次都创建一个并丢弃它.但这意味着,我需要缓存 5000 个(或者将来可能更多).

should I cache these dataSource objects in my Spring application, so I dont have to create one each time and discard it. but this implies, I need to cache 5000 (or probably more in the future).

Spring 文档

DataSource 应始终配置为 Spring IoC 容器中的 bean.在第一种情况下,bean 直接提供给服务;在第二种情况下,它被提供给准备好的模板.

所以这让我的事情变得更难了.

so that makes things harder for me.

谢谢

推荐答案

您可以定义一个具有范围原型的 bean myBean 并使用 BeanFactory 的 getBean(String name, Object... args) 方法.args 将是发送到构造函数的 args(在您的情况下,这些将是 db 连接).bean 将返回一个 jdbcTemplate,该模板使用从连接属性定义的数据源构造.该模板可以进一步用于其他类.

You can define a bean myBean with scope prototype and use the getBean(String name, Object... args) method of a BeanFactory. args would be the args sent to the constructor (in your case these would be db connections). The bean would return a jdbcTemplate constructed with a datasource defined from the connection properties. This template can be further used in other classes.

由于bean的作用域是prototype,创建的实例会在当前对象被使用后被垃圾回收.如果您有内存限制,这会有所帮助,创建对象方面的真正繁重工作是在获取实际数据库连接时完成的.在大量重复使用连接的情况下,缓存将是一个很好的解决方案.

Since the scope of the bean is prototype, the created instances will be garbaged collected after the current object is used. This can help if you have memory constraints, the really heavy lifting in terms of creating objects is done when getting the actual DB connections. Caching would be a good solution in case of heavy re-usage of connections.

在此处查看此 bean 和方法用法的示例:具有动态构造函数值的 spring bean

See an example of this bean and method usage here: spring bean with dynamic constructor value

这篇关于如何在 spring 中拥有一个不是 bean 的数据源对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆