Framework Spring/Fichier properties

Afin de configurer facilement l'application, on peut faire appel à un fichier texte de propriété, souvent avec l'extension properties :

Le fichier MainClass :

package mainPackage;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass
{
	public static void main(String[] args)
	{
		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("Beans.xml");
		PropertyDisplayer propertyDisplayer = classPathXmlApplicationContext.getBean(PropertyDisplayer.class);
		propertyDisplayer.showProperties();
		classPathXmlApplicationContext.close();
	}
}

Il est le point d'entrée du programme. Il lit le fichier de configuration xml récupère le bean d'affichage de propriété et demande l'affichage

package mainPackage;

import java.io.IOException;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertyDisplayer
{
	public void showProperties()
	{
		ClassPathResource classPathResource = new ClassPathResource("application.properties");
		try
		{
			Properties props = PropertiesLoaderUtils.loadProperties(classPathResource);
			System.out.println("Property : " + props.getProperty("property"));
		} 
		catch (IOException ioException)
		{
			ioException.printStackTrace();
		}
	}
}

Dans PropertyDisplayer, méthode showProperties on charge le fichier application.properties situé par défaut dans le class path et on affiche le contenu de la propriété appelée "property"


Le pom.xml est configuré de la manière suivante :

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SimpleJavaProject</groupId>
	<artifactId>SimpleJavaProject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<springframework.version>5.1.5.RELEASE</springframework.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${springframework.version}</version>
		</dependency>
	</dependencies>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
				<configuration>
					<syntaxhighlight>1.8</ source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Le fichier application.properties contient simplement :

property=hello world

Le résultat de l'execution est l'affichage du contenu de property :

Property : hello world