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... Learning Spring requires knowledge of Java core, Intellij IDEA Ultimate and a little patience. The course will consist of several lessons. Each session 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 a summary, 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 as Inversion of Control (IoC) and Dependency Injection (DI). IoCis 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". Thus, dependencies between objects will be minimized. 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 rest of the parts, you can leave the default values ​​\u200b\u200band change it at your discretion (except for the version): Spring Course - IoC / DI - 2 After that, the project will open with something like this structure: Spring Course - IoC / DI - 3 The main folder contains packages, codes and files for 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 store classes with testing. We will not touch on this topic. One of the most important files is the pom.xml file. This file will describe Spring dependencies for our project. It is with this file that we will begin our work. Let's open this file first. 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 site mvnrepository.com , which contains all the dependencies for managing the 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 another). Then we select the version, preferably the RELEASE version. After that, approximately the following window will open: Course Spring - IoC / DI - 5 We are interested in the part that is highlighted in red. 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. Spring beans are used to create beans. I'll talk about bins a little later. After adding the dependencies, an icon will appear in the upper right corner: Spring Course - IoC / DI - 6 Click on it and wait for Idea to download 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 here . My GitHub account Course content To be continued...
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION