【问题标题】:Configuring the database properties from outside of intellij从 intellij 外部配置数据库属性
【发布时间】:2017-01-05 08:52:34
【问题描述】:

我想问一下,目前我在 intellij 的持久层中有我的数据库属性,例如用户名和密码。但我想把它放在外面的某个地方,所以如果有人想更改密码或数据库内的任何配置,他不应该在我当前的结构中挖掘。现在我的结构是持久性,然后是 main,然后是资源,然后是 dbconfig 属性,所以有什么办法可以做到。

【问题讨论】:

  • intellij 里面是什么意思?您是否使用 spring boots 应用程序属性文件来定义数据源?
  • 是的,我用的是弹簧靴

标签: java intellij-idea spring-boot


【解决方案1】:

您可以在resources 文件夹中创建一个文件app.properties,其中包含您需要的所有数据库信息:

# Datasource details
testapp.db.driver = org.h2.Driver
testapp.db.url = jdbc:h2:mem:test
testapp.db.username = username
testapp.db.password = password

然后你可以在你的Java代码中引用它:

@Configuration
@PropertySource("app.properties")
public class DataConfig {
    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(env.getProperty("testapp.db.driver"));
        ds.setUrl(env.getProperty("testapp.db.url"));
        ds.setUsername(env.getProperty("testapp.db.username"));
        ds.setPassword(env.getProperty("testapp.db.password"));
        return ds;
    }
}

【讨论】:

  • 嗨 DimaSan,谢谢你的建议..我想知道我应该为这个 app.properties 选择资源包然后为这个 app.properties 生成 2 个文件,我应该把我的数据库详细信息放在其中一个?
  • 嗨@siya,是的,您也可以使用 ResourceBundle 来完成,检查good example。但就我个人而言,这个场景更清晰,更易于使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2017-08-14
  • 1970-01-01
  • 2011-01-23
  • 2020-07-07
  • 2014-06-23
  • 1970-01-01
相关资源
最近更新 更多