Introduction
In this project, you will learn how to set up a Spring development environment using Maven and the Spring Framework version 5.3.7. This project will guide you through the process of creating a Maven project, configuring the Spring context, and writing a test class to verify the successful setup of the Spring environment.
👀 Preview
- The directory structure should look like this:

- The Spring environment was set up correctly:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.labex:springenv >-------------------
[INFO] Building springenv 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springenv ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springenv ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ springenv ---
[INFO] Surefire report directory: /home/labex/project/springenv/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.labex.TestSpring
The Spring environment was built successfully!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.662 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.270 s
[INFO] Finished at: 2023-04-10T10:00:00Z
[INFO] ------------------------------------------------------------------------
🎯 Tasks
In this project, you will learn:
- How to set up a Maven project for the Spring environment
- How to create the Spring configuration file
applicationContext.xml - How to write a test class to validate the Spring environment setup
🏆 Achievements
After completing this project, you will be able to:
- Create a Maven project and manage dependencies using the
pom.xmlfile - Configure the Spring context using the
applicationContext.xmlfile - Write a test class to ensure the proper setup of the Spring environment
Configurez le projet Maven
Dans cette étape, vous allez apprendre à configurer un projet Maven pour l'environnement Spring.
Ouvrez votre IDE ou éditeur de texte préféré et accédez au répertoire
~/projet/.Vous verrez un projet Maven appelé
springenv. La structure de répertoires devrait ressembler à ceci :~/projet/springenv/ ├── pom.xml └── src/ ├── main/ │ └── java/ └── test/ └── java/Ouvrez le fichier
pom.xmlet ajoutez les dépendances suivantes :<?xml version="1.0" encoding="UTF-8"?> <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>org.labex</groupId> <artifactId>springenv</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- Spring Framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
Ce fichier pom.xml configure les dépendances nécessaires pour la version 5.3.7 du Spring Framework et le framework de test JUnit.
Créez le fichier de configuration Spring
In this step, you will create the Spring configuration file applicationContext.xml.
In the
src/main/resources/directory, you will see a file calledapplicationContext.xml.Add the following content to the
applicationContext.xmlfile:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Your bean configurations go here --> </beans>This file sets up the root
<beans>element and includes the necessary schema rule file for the Spring configuration.
Créez la classe de test
In this step, you will create a test class to verify the successful setup of the Spring environment.
In the
src/test/java/directory, you will see a Java class calledTestSpring.Add the following code to the
TestSpringclass:import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.Assert.assertTrue; public class TestSpring { @Test public void testSpringEnvironment() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("The Spring environment was built successfully!"); context.close(); // Use the assertTrue method to ensure that the test passes. assertTrue(true); } }This test class creates a
ClassPathXmlApplicationContextusing theapplicationContext.xmlfile and prints a message to the console to confirm that the Spring environment was set up successfully.
Exécutez le test
Dans cette étape finale, vous allez compiler et exécuter les tests pour vérifier la configuration de l'environnement Spring.
Ouvrez un terminal et accédez au répertoire
~/projet/springenv/en utilisant la commande suivante :cd ~/projet/springenv/Exécutez la commande suivante pour compiler et exécuter les tests :
mvn testVous devriez voir la sortie suivante :
[INFO] Scanning for projects... [INFO] [INFO] ------------------< org.labex:springenv >------------------- [INFO] Building springenv 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springenv --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springenv --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ springenv --- [INFO] Surefire report directory: /home/labex/projet/springenv/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.labex.TestSpring The Spring environment was built successfully! Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.662 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.270 s [INFO] Finished at: 2023-04-10T10:00:00Z [INFO] ------------------------------------------------------------------------La sortie indique que les tests ont été exécutés avec succès et que l'environnement Spring a été correctement configuré.
Félicitations! Vous avez terminé le projet de configuration de l'environnement de développement Spring avec la version 5.3.7.
Résumé
Félicitations! Vous avez terminé ce projet. Vous pouvez pratiquer plus de laboratoires sur LabEx pour améliorer vos compétences.



