java - 实例化 Spring Bean
问题描述
我正在尝试根据我的要求调用两个不同的 bean。这两个 bean 实现了相同的接口。我必须使用两个基于数据中心的不同 db。我如何通过从 application.properties 传递参数在 springboot 应用程序中做到这一点
interface Data
{
string getData(String query)
}
@Component("oracle")
class Oracle implements Data
{
//getMethod Code Here.
}
@Component("sqlserver")
class SqlServer implements Data
{
//getMethod Code Here.
}
目前我正在使用
String db = appContext.getBean(propertiesfile.db//getting the value from properties file, Data::class.java)
并根据返回值调用特定的数据库。但是无论如何我只能基于 app.properties 实例化一个 bean 意味着不在appContext.getBean
每个请求中使用
解决方案
根据我最终使用的建议@ConditionalOnProperty
这个链接帮助我解决了这个问题。
推荐阅读
- python - 如何运行当我的程序结束 python 时不退出的 bash 文件
- amazon-web-services - AWS 配置历史
- html - 使用 flex 属性时,图像不随其父 div 缩放
- python - 如何在具有 Redis 排队作业的 Flask 应用程序中使用 PostgreSQL 和 SQLAlchemy
- javascript - 阻止所有 cookie 时网站崩溃
- c# - 如何将 DataReceivedHandler 从 Arduino 延迟到 WPF
- azure - 为什么在我的 Azure 应用服务中安装了 python 3.6 版后,python 仍然是 2.7 版?
- javascript - 在其他文件中使用其他文件中的导入模块
- opengl - 如何在不需要任何窗口系统库的情况下使用 OpenGL 渲染到内存?
- sybase - sybase插入数据时出现invalid column name error是什么意思?