2

I'm working on the REST API with spring boot. I want to use git in my project. in the file application.properties I have the database Url, username and password that I do not want to push on git. I don't know how can I create a file which contains my database configuration and how to inject those configurations in the application.properties .

application.properties

## Server Properties
server.port= 5000

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url= jdbc:mysql://localhost:3306/MyApp?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username= user
spring.datasource.password= pass
Achraf
  • 347
  • 1
  • 5
  • 17

2 Answers2

3

Spring picks up configuration properties not only from the application.properties but also from command line arguments, JAVA System-properties or from environmental-variables.

See complete list here: Spring Externalized Configuration.

So - for reference - you can keep the properties in the application.properties file with some default values (like in your example) in order to let other users know what kind of properties they can set for your application.

But instead of setting your real values there, you can either pass the variable to your application as arguments, like

-Dspring.datasource.username=user -Dspring.datasource.password= pass

or you can set them as environmental variables.

You can even create multiple configuration with different settings. If Spring cannot find a variable in the current configuration, then it will pick it up from application.properties (or from the other sources - see above)

Selindek
  • 3,269
  • 1
  • 18
  • 25
0

first you should add application.properties to .ignore file like this

application.properties 

if you will just connect to database you won't need to inject values by hand you just write it in application.properties
but if you want to put values in properties file and use it in Application

package com.microservice.test.limitservice;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("limit-service")
public class Configuration {
private int minimum;
private int maximum;

public int getMinimum() {
    return minimum;
}
public void setMinimum(int minimum) {
    this.minimum = minimum;
}
public int getMaximum() {
    return maximum;
}
public void setMaximum(int maximum) {
    this.maximum = maximum;
}

}

and how to inject it simply

@Autowired
private Configuration configuration;

the application.properties file could be like this

limit-service.minimum=56333445
limit-service.maximum=6500

you should notice that it start with as example limit-service and @ConfigurationProperties("**limit-service**")

And if you want to store your configuration in application.properties secure you can see this link Spring Boot how to hide passwords in properties file

Shaaban Ebrahim
  • 9,766
  • 1
  • 18
  • 20
  • i don't think you got me . Actually i do not want to push application.properties to git because it contains database url ,username and password. i'm looking for a way to put database url ,username and password in a file that i will gitignore (if it is the right way to do it). – Achraf Oct 12 '18 at 20:46
  • you can ignore application.properties too – Shaaban Ebrahim Oct 12 '18 at 20:50
  • @Achraf maybe this link is good for you https://stackoverflow.com/questions/37404703/spring-boot-how-to-hide-passwords-in-properties-file – Shaaban Ebrahim Oct 12 '18 at 20:54
  • i looked for spring boot gitignore files example no one had application.properties. there will not be a problem when someone pull the project – Achraf Oct 12 '18 at 20:55
  • maybe every one in team has his own application.properties , but sure it may cause problems when working with no application.properties – Shaaban Ebrahim Oct 12 '18 at 20:57
  • @Achraf please if you see this answer is helpful and correct mark up vote and choose it as a correct answer . – Shaaban Ebrahim Oct 12 '18 at 21:02