JavaRush /Java Blog /Random EN /Spring. Lesson 1. IoC/DI
Umaralikhon
Level 3
Красноярск

Spring. Lesson 1. IoC/DI

Published in the Random EN group
And so... To learn Spring you will need knowledge of Java core, Intellij IDEA Ultimate and a little patience. The course will consist of several lessons. Each lesson will cover a different topic. I would also highly recommend writing notes. Yes, no one will give you credit for the semester for this. It’s just that when writing notes, all parts of the brain designed to remember information work. Enough water. Begin. Inversion of Control (IoC) && Dependency Injection (DI) First, let's define the two most important terms, Inversion of Control (IoC) and Dependency Injection (DI). IoC is an OOP principle used to reduce coupling between classes and objects. The programmer will place the necessary code at the right points in the program and does not worry about how and when the placed code should work. In simple terms, when using IoC, the code will be managed by the framework and not by the programmer. DI - makes application objects weakly dependent on each other. That is, an external mechanism developed by the programmer will take care of the initialization of objects. When using DI, the programmer will work not at the “class level” but at the “interface level”. This way, dependencies between objects will be kept to a minimum. Let's practice First, let's create a simple maven project. To do this, select the “Create new Project” item and select “Maven” from the menu: Spring Course - IoC/DI - 1 In the remaining parts, you can leave the default values ​​or change them at your discretion (except for the version): Spring Course - IoC/DI - 2 After which a project will open with approximately the following structure: Spring Course - IoC/DI - 3 The main folder stores packages and codes and files for running the main part of the program. Packages (in our case org.example) will store Java code. The resources folder will contain configuration files or resource files. It is in this folder that we will create the applicationContext.xml file, in which we will describe our beans (more on this later). The Test folder will contain testing classes. We will not touch on this topic. One of the most important files is the pom.xml file. This file will describe the Spring dependencies for our project. It is with this file that we will begin our work. First, let's open this file. The file will look something like this:
<?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.example</groupId>
    <artifactId>IoC</artifactId>
    <version>1.0-SNAPSHOT</version>

  <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>
We will add the necessary dependencies to this file. To do this, go to the website mvnrepository.com , which contains all the dependencies for managing a maven project. In the search part we type "Spring context". Among the list of results, select “Spring context” (namely “Spring context” from apache and not something else). Then select the version, preferably the RELEASE version. After which the following window will open: Spring Course - IoC/DI - 5 We are interested in the part that is highlighted in red. Let's copy the code and paste it inside the <dependencies> </dependencies> tag. We perform the same actions for the “Spring bean” and “Spring core” dependencies. As a result, the pom.xml file should look like this:
<?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.example</groupId>
    <artifactId>IoC</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>
Briefly about everything. Spring core stores all the basic operations for the framework to work. Spring context - creates and stores class objects for DI/IoC work. And Spring beans are used to create beans. I'll tell you about the bins a little later. After adding dependencies, an icon will appear in the upper right corner: Spring Course - IoC/DI - 6 Click on it and wait until Idea downloads all these dependencies. After downloading, in the project folder "External Libraries" we can see the downloaded .jar files that contain Spring dependencies: Spring Course - IoC/DI - 7 This concludes the first lesson of our course. Thus we briefly learned:
  • What is IoC/DI
  • How to create Spring projects
  • How to set them up
The full source code can be found at this link . My GitHub account Course content To be continued...
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION