Achraf
Achraf

Reputation: 357

application.properties spring boot value injection

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

Upvotes: 2

Views: 4897

Answers (2)

Selindek
Selindek

Reputation: 3423

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)

Upvotes: 3

Shaaban Ebrahim
Shaaban Ebrahim

Reputation: 10422

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

Upvotes: 0

Related Questions