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

Spring. Lesson 3. DI using setter

Published in the Random EN group
And so... We continue our course on Spring. In the last lesson we learned how to inject dependencies using a class constructor. Now it's time to introduce you to another method of dependency injection - using a setter . To restore memory of previous lessons, I will show you the structure of our program. Spring.  Lesson 3. DI using setter - 1 We are only interested in the HiringDepartment and applicationContext files. They manage our project. We will redo them. This is the old code for the HiringDepartment file: (Listing 1)
package org.example;

public class HiringDepartment {
    private Development development; //Определяем интерфейс

    //Конструктор принимает an object интерфейса
    public HiringDepartment(Development development){
        this.development = development;
    }

    public void displayInfo(){
        System.out.println("Name: " + development.getName());
        System.out.println("Job: " + development.getJob());
    }
}
As we see here, a constructor is used to initialize an object of type Development. We remove this constructor (or leave it, it makes no difference). And let's write a setter, as in the following example: (Listing 2)
package org.example;

public class HiringDepartment {
    private Development development; //Определяем интерфейс

//Вместо конструктора используем сеттер
    public void setDevelopment(Development development){
        this.development = development;
    }

    public void displayInfo(){
        System.out.println("Name: " + development.getName());
        System.out.println("Job: " + development.getJob());
    }
}
Now let's open the applicationContext.xml file in the resources folder. It looks like this: (Listing 3)
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="javaDeveloper" class="org.example.JavaDevelopment"/>
    <bean id="pythonDeveloper" class="org.example.PythonDevelopment"/>

    <bean id="hiringDepartment" class="org.example.HiringDepartment">
        <constructor-arg ref="javaDeveloper"/>
    </bean>

</beans>
As we remember (or don’t remember), in the line <constructor-arg ref="javaDeveloper"/> the dependency is injected using the constructor. Now we need to rewrite this line for the setter as shown below:
<bean id="javaDeveloper" class="org.example.JavaDevelopment"/>
<bean id="pythonDeveloper" class="org.example.PythonDevelopment"/>

<bean id="hiringDepartment" class="org.example.HiringDepartment">
    <property name="development" ref="pythonDeveloper"/> <!--Изменяем только тут-->
</bean>
What exactly is going on here? The <property name="development" ref="pythonDeveloper"/> line uses the " property " keyword to inject the dependency using a setter. How did Spring find out which setter the pythonDeveloper object needs to be associated with in our example ? Note the resulting property="..." value . It is written as "development" . And our setter in the HiringDepartment file is called setDevelopment . Spring converts the value of development to setDevelopment behind the scenes . That is, the first letter becomes capitalized and the prefix set (developmet -> setDevelopment) is placed at the beginning of the word. Spring.  Lesson 3. DI using setter - 2What's under the hood? What we see: Two beans are created with id javaDeveloper and pythonDeveloper:
<bean id="javaDeveloper" class="org.example.JavaDevelopment"/>
<bean id="pythonDeveloper" class="org.example.PythonDevelopment"/>
What actually happens: Spring creates two objects of type JavaDevelopment and PythonDevelopment:
JavaDevelopment javaDevelopment = new JavaDevelopment();
PythonDevelopment pythonDevelopment = new PythonDevelopment();
And here is what we see: A bean with id hiringDepartment is created. Then the id of one of our above created objects is assigned to property:
<bean id="hiringDepartment" class="org.example.HiringDepartment">
    <property name="development" ref="pythonDeveloper"/>
</bean>
What actually happens: An object of type HiringDepartment is created. Then the set method of this class setDevelopment(Development development) is called. And the ref value (id of the created objects) is passed as an argument:
HiringDepartment hiringDepartment = new HiringDepartment();
hiringDepartment.setDevelopment(pythonDevelopment);
Launch the application:
Name: Mike
Job: Middle Python developer
That's all I have. Thank you for attention! Source code at the link My GitHub Cart Course content To be continued...
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION