java - 是否可以区分我在springboot中加载的数据库类型?(甲骨文与 Azure SQL)
问题描述
我试图找出如何判断我是从 bean 初始化加载 Oracle 还是 Azure SQL。目前,我必须决定在我的 helm 图表中加载哪个数据库,Oracle 或 Azure SQL。然后,我需要知道要加载哪个 xml 文件,因为查询语法略有不同。我想知道是否有办法在不使用硬编码值的情况下做到这一点。这是我的数据库 bean 配置文件:
@Configuration
@EnableConfigurationProperties
public class ProjectDataSourceConfig {
public static final String DB_TX_MANAGER = "";
@Bean
@Primary
@ConfigurationProperties("datasource.project")
public DataSourceProperties projectDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
public DataSource projectDataSource() {
return projectDataSourceProperties().initializeDataSourceBuilder().type(ComboPooledDataSource.class).build();
}
@Bean
public NamedParameterJdbcTemplate projectJdbcTemplate() {
return new NamedParameterJdbcTemplate(projectDataSource());
}
@Bean(name = DB_TX_MANAGER)
public DataSourceTransactionManager projectDbtransactionManager() {
return new DataSourceTransactionManager(projectDataSource());
}
}
如果可能的话,我还需要根据我正在加载的数据库加载不同的 xml 查询文件。例如,如果我正在加载 Oracle,我希望我的存储库使用 oracle.xml 文件。这是我当前的存储库类:(我目前正在使用 oracle)
@Configuration
@Import(ProjectDataSourceConfig.class)
@PropertySource("classpath:event-sql-oracle.xml")
public class eEventRepositoryConfig {
@Value("${EventRepository.insertEvent}")
private String insertEvent;
@Bean
public EventRepository<Event> eventRepository(final NamedParameterJdbcTemplate jdbcTemplate) {
return new EventRepository(jdbcTemplate, insertEvent);
}
}
解决方案
我不知道我是否可以在创建 bean 时说出来,但我可以使用 .properties 或 helm chart 来指示 db 类型。
env:
db.type=oracle
然后将其用作占位符,@PropertySource
如下所示:
@Configuration
@Import(ProjectDataSourceConfig.class)
@PropertySource("classpath:event-sql-${db.type}.xml")
public class eEventRepositoryConfig {
我将不得不更改db.type
,因为我正在更改我的配置中的服务器类型。
推荐阅读
- elasticsearch - 我是 ES 新手,在 ES 中有一个多重匹配查询,并希望根据其可用性考虑字段
- windows - 带有 LOCALE_SABBREVLANGNAME 的 GetLocaleInfo 是否已弃用?
- python - 用于 ?在正则表达式中的命名组中
- javascript - 如何将项目拖放到 react-dnd 中的空白部分?
- python - 数据帧长度 PySpark
- centos - CentOS EPEL fail2ban 不处理 tomcat 的 systemd 日志
- image - 图像在组件重新渲染时消失
- java - 生成任意数量的视图
- c# - 泛型类型参数 C# - 如何泛型类返回类型
- python - 如何随着时间的推移将多个复杂的信号发送到同一个时隙