Lire les valeurs du fichier application.properties dans Spring Boot
Cet article explique comment lire une valeur définie dans le fichier application.properties dans une application Spring Boot.
Dans Spring Boot, nous pouvons utiliser des fichiers de propriétés, des fichiers YAML, des variables d'environnement et des arguments de ligne de commande pour externaliser notre configuration. Ceci est utile lorsque vous travaillez avec le même code d'application dans différents environnements. Cet article expliquera comment lire une valeur définie dans les fichiers de propriétés.
1. Annotation @Value
Le moyen le plus simple est d'utiliser le @Value
annotation pour charger des variables à partir du application.properties
.
Supposons dans application.properties
, nous avons une propriété project.name
. Ensuite, les valeurs de propriété peuvent être injectées directement dans vos beans en utilisant le @Value
annotation, comme indiqué ci-dessous :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ProjectDetails { @Value("${project.name}") private String projectName; public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } } |
Code contrôleur :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller { @Autowired public ProjectDetails bean; @RequestMapping("projectName") String getProjectName() { return bean.getProjectName(); } } |
2. Abstraction de l'environnement du printemps
Les valeurs de propriété sont également accessibles via l'abstraction de l'environnement de Spring, comme illustré dans l'exemple suivant :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class ProjectDetails implements EnvironmentAware { private String projectName; @Override public void setEnvironment(Environment environment) { this.projectName = environment.getProperty("project.name"); } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } } |
Une autre alternative consiste simplement à injecter Environment dans notre contrôleur/bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class ProjectDetails { @Autowired private Environment environment; public String getProjectName() { return environment.getProperty("project.name"); } } |
Il s'agit de lire les valeurs du fichier application.properties dans Spring Boot.