java - how to extenalize db connection stuff
问题描述
I am new in Java Spring framework and related technologies. I want to externalize the database stuff like connection string, username and password in a different file so that incase there is change in the database username and or password, I can go an change without touching the war file and recompiling the application. Right now all the application I am supporting was hardcoded Any help will be appreciated. NB we are oracle shop
解决方案
使用properties
文件。这是配置Oracle
数据库的示例:
spring.datasource.url=jdbc:oracle:thin:@localhost:1522:orcl
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
并在您的配置类文件中使用类似:
@Primary
public DataSource userDataSource() {
DriverManagerDataSource dataSource
= new DriverManagerDataSource();
dataSource.setDriverClassName(
env.getProperty("spring.datasource.driver.class"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
根据您的需要,网络上有很多教程。检查例如: https ://www.programmergate.com/spring-boot-jpa-hibernate-oracle/
推荐阅读
- python - SendBird 错误 400403 无效值:JSON 正文
- python - 定义和求解无冗余方程(Python)
- scala - 使用功能构造的组合批处理
- flutter - Flutter - Ontap InkWell 在 Stack 中不起作用
- c# - C# 应用程序是否可以读取其他应用程序发送的通知并相应地执行操作?
- swift - videoGravity resizeAspectFill 不起作用 - Swift - AVFoundation
- javascript - MapBox 通过 props 设置多个标记 react
- dns - 如何在没有网页的情况下拥有域名?
- sql - oracle中如何通过不同角色在其他列上获取不同列的值
- c++ - 在 C++ 中将向量转换为嵌套映射