我正在尝试编写一个简单的 Hibernate 应用程序,并且希望根据应用程序配置文件更改 SQLite 数据库位置。
因此,我想做的是从项目中的文本文件中检索数据库的路径并将其放入 dbLocation
中,然后运行以下代码:
Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", "jdbc:sqlite:" + dbLocation);
是否有更好、更“标准”的方法来做到这一点?我正在为我的应用程序使用 Spring Boot,我刚刚意识到有一个名为 application.properties
的文件。我可以用这个吗?我对 Hibernate 和 Spring 框架都很陌生。
最佳答案
在 Spring Boot 应用程序属性中,您可以外部化应用程序属性,以便在应用程序源代码之外配置/管理它们。
在 application.properties 中定义属性后,您可以使用 SpringBoot 内置功能来访问值
@Configuration
public class ApplicationProperty {
@Value("${prop}")
private String prop;
关于java - Hibernate/Spring框架: path of the database in a configuration file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719178/