0

I am trying to write a simple Hibernate application and I want the SQLite database location to be changed according to an application configuration file.

What I was trying to do was therefore retrieving the path of the database from a text file in the project and putting it inside dbLocation, then running the following piece of code:

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

Is there a better and more "standard" way to do so? I'm using Spring Boot for my application and I just realized there's afile called application.properties. May I use this one maybe? I'm pretty new to both Hibernate and the Spring framework.

Robb1
  • 4,587
  • 6
  • 31
  • 60
  • Does this answer your question? [Database application.yml for Spring boot from applications.properties](https://stackoverflow.com/questions/33323837/database-application-yml-for-spring-boot-from-applications-properties) – Compass Jan 13 '20 at 15:28
  • You can use either .props or .yml for your configuration. Spring Boot will also split between environments so you can potentially use local/dev/qa/prod files selected at start-up time. – Compass Jan 13 '20 at 15:29

2 Answers2

0

In Spring Boot application properties you can externalize your application properties so they are configured/managed outside your application source code.

Once a property is defined in application.properties you can use SpringBoot built-in feature to access the values

@Configuration 
public class ApplicationProperty {

@Value("${prop}")
private String prop;
Beppe C
  • 11,256
  • 2
  • 19
  • 41
0

With Spring Boot another way is to use @ConfigurationProperties.

Example from the documentation can be found here

A comparison between @ConfigurationProperties and @Value annotation from the documentation

Feature          @ConfigurationProperties   @Value

Relaxed binding      Yes                     No
Meta-data support    Yes                     No
SpEL evaluation      No                      Yes
R.G
  • 6,436
  • 3
  • 19
  • 28