Hibernate / Spring框架:配置文件中数据库的路径

时间:2020-01-13 15:05:59

标签: java spring hibernate sqlite spring-boot

我正在尝试编写一个简单的 Hibernate 应用程序,并且希望根据应用程序配置文件来更改SQLite数据库位置。

因此,我试图从项目中的文本文件中检索数据库的路径,并将其放入dbLocation中,然后运行以下代码:

Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", "jdbc:sqlite:" + dbLocation);

是否有更好,更“标准”的方法?我在应用程序中使用了 Spring Boot ,但我刚刚意识到有一个名为application.properties的文件。我可以用这个吗?我对Hibernate和Spring框架都是陌生的。

2 个答案:

答案 0 :(得分:0)

在Spring Boot应用程序属性中,您可以外部化应用程序属性,以便在应用程序源代码之外对其进行配置/管理。

一旦在application.properties中定义了属性,就可以使用SpringBoot内置功能访问值

@Configuration 
public class ApplicationProperty {

@Value("${prop}")
private String prop;

答案 1 :(得分:0)

使用Spring Boot的另一种方法是使用Series.str.extract

可以从文档中找到示例@ConfigurationProperties

来自here

的@ConfigurationProperties和@Value注释之间的比较
Feature          @ConfigurationProperties   @Value

Relaxed binding      Yes                     No
Meta-data support    Yes                     No
SpEL evaluation      No                      Yes
相关问题