0

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.

juldeh
  • 129
  • 13
  • If you put the properties file in the same level as your produced `.jar`, you should be able to pick up those properties. – Sofo Gial Apr 12 '19 at 08:14

1 Answers1

1

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);
   }
}
kSp
  • 224
  • 2
  • 13
  • IMHO, not like this, as I know you need a different PropertyLoader for YAML (but could be that it was implemented in the past, I didn't read in the documentation neither I tried). An example would be in this Post: https://stackoverflow.com/questions/21271468/spring-propertysource-using-yaml – kSp Apr 12 '19 at 11:51