juldeh
juldeh

Reputation: 149

Import spring properties from an external file located in the server filesystem

I need to import Spring properties (in Spring Boot) as spring.datasource, server.port... from a file that is located in the file system (out of the java application).

This is for a Spring Boot application that needs to connect to a database.

spring:
    datasource:
        url: jdbc:oracle:thin:@X.X.X.X:XXXX:XXXX
        username: XX
        password: XX
        driver-class-name: oracle.jdbc.driver.OracleDriver
        hikari:
            connection-timeout: 60000
            maximum-pool-size: 5
    application:
        name: XX
server:
    port: 9000
    contextPath: /
    servlet:
        session:
            cookie:
                http-only: true
                secure: true

By the moment I am not able to import properties from file using @PropertySource(value = "C:/test.properties") in class.

Upvotes: 0

Views: 448

Answers (1)

kSp
kSp

Reputation: 313

There are multiple ways to achieve this. My preferred one is to annotate your applications main class with @PropertySource and configure it to read your property file.

Example:

@SpringBootApplication
@PropertySource({
        "file:C:\test.properties"
})
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }
}

Upvotes: 1

Related Questions