首页 > 解决方案 > 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

标签: javaoraclespring-mvc

解决方案


使用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/


推荐阅读