Configuration de l'environnement de développement Spring

JavaJavaBeginner
Pratiquer maintenant

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

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:
Directory Structure
  • 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.xml file
  • Configure the Spring context using the applicationContext.xml file
  • Write a test class to ensure the proper setup of the Spring environment

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("Java")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["Object-Oriented and Advanced Concepts"]) java(("Java")) -.-> java/ConcurrentandNetworkProgrammingGroup(["Concurrent and Network Programming"]) java(("Java")) -.-> java/SystemandDataProcessingGroup(["System and Data Processing"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("Classes/Objects") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("Packages / API") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("OOP") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("Exceptions") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/jdbc("JDBC") java/ConcurrentandNetworkProgrammingGroup -.-> java/working("Working") java/SystemandDataProcessingGroup -.-> java/xml_dom4j("XML/Dom4j") subgraph Lab Skills java/classes_objects -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} java/packages_api -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} java/oop -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} java/exceptions -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} java/jdbc -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} java/working -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} java/xml_dom4j -.-> lab-300398{{"Configuration de l'environnement de développement Spring"}} end

Configurez le projet Maven

Dans cette étape, vous allez apprendre à configurer un projet Maven pour l'environnement Spring.

  1. Ouvrez votre IDE ou éditeur de texte préféré et accédez au répertoire ~/projet/.

  2. Vous verrez un projet Maven appelé springenv. La structure de répertoires devrait ressembler à ceci :

    ~/projet/springenv/
    ├── pom.xml
    └── src/
        ├── main/
        │   └── java/
        └── test/
            └── java/
  3. Ouvrez le fichier pom.xml et 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.

✨ Vérifier la solution et pratiquer

Create the Spring Configuration File

In this step, you will create the Spring configuration file applicationContext.xml.

  1. In the src/main/resources/ directory, you will see a file called applicationContext.xml.

  2. Add the following content to the applicationContext.xml file:

    <?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.

✨ Vérifier la solution et pratiquer

Create the Test Class

In this step, you will create a test class to verify the successful setup of the Spring environment.

  1. In the src/test/java/ directory, you will see a Java class called TestSpring.

  2. Add the following code to the TestSpring class:

    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 ClassPathXmlApplicationContext using the applicationContext.xml file and prints a message to the console to confirm that the Spring environment was set up successfully.

✨ Vérifier la solution et pratiquer

Exécutez les tests

Dans cette étape finale, vous allez compiler et exécuter les tests pour vérifier la configuration de l'environnement Spring.

  1. Ouvrez un terminal et accédez au répertoire ~/projet/springenv/ en utilisant la commande suivante :

    cd ~/projet/springenv/
  2. Exécutez la commande suivante pour compiler et exécuter les tests :

    mvn test

    Vous 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.

✨ Vérifier la solution et pratiquer

Sommaire

Félicitations! Vous avez terminé ce projet. Vous pouvez pratiquer plus de laboratoires sur LabEx pour améliorer vos compétences.