[JAVA] DI: What is Dependency Injection?

Purpose of this page

I write it to convince myself because it doesn't look right even if I look at various explanations.

DI overview

First of all, if it is a textbook style

"DI realizes" Inversion of Control Principle ", which is one of the design patterns [^ 1]". You can separate the instantiation and dependency resolution that make up a component from the source code. (From a thorough introduction to Spring by NTT DATA Corporation)

I don't know what you're talking about. For the time being, let's think from a place that is easy to understand.

[^ 1]: Collection of design know-how in object-oriented languages

Benefits of DI

For the time being, what to do is aside, what can be improved by DI?

・ Reduced dependency between classes = Loose coupling between classes (becomes loose coupling) ・ ↑ makes it easy to reuse classes, etc. ・ ↑ makes unit tests easier

It seems that. There seems to be a core here because the word "dependency" has come up many times. So what are "dependencies" in programming?

What is dependency

If you use textbook words, "One module changes or depends on the internal behavior of another module = If you change the specifications of a dependent module, the contents of the module are likely to be corrupted."

What is a configuration with high dependency in Java language etc. -A new instance of another class is created in the method of one class. -Using an instance of another class as an argument of a method of one class And so on. If it is a concrete code

Hoge.java


class Hoge{
    public void hoge(){
        Fuga fuga = new Fuga();
    }
}

It has become quite concrete. If you use a new class in one class, and if there is a change in the constructor of the new class, you can imagine that the method of the caller must also be changed. In addition, if you change the method in the class that creates the instance, the processing of the caller will be messed up. The solution to this problem is to make the calling class use the interface instead of creating an instance by newing the implementation class, and make the new class a class that inherits the interface. That is.

Fuga.java


pulic interface Fuga{
    int a;
    void foo();
}

HogeFuga.java


pulic HogeFuga implements Fuga{
    int a;
    void foo(){
        fugafuga;
    };
}

Hoge.java


class Hoge{
    public void hoge(){
        Fuga fuga = new HogeFuga();
    }
}

By using this method, you can freely use the class that implements the interface, and you can reduce the dependency. However, since the interface class cannot be new after all, it is necessary to describe the implemented subclass to be new at the caller.

Therefore, describe the relationship using xml files and annotations. ↓ This is "DI"

Specific DI method

Setter injection

Hoge.java


class Hoge{
    Fuga fuga;
    public void hoge(){
    }
    public void setFuga(Fuga fuga){
         this.fuga = fuga;
    }
}

At this time, any class that implements the Fuga class can be passed to the setFuga class, and the Hoge class does not care which class is actually used.

Annotation injection

Is this the mainstream when using Spring?

Hoge.java


class Hoge{
    @Autowired
    protected Fuga fuga
    public void hoge(){
    }
}

Summary

What is DI? = I want to reduce the binding of the class I use!

Recommended Posts

What is DI (Dependency Injection)
DI: What is Dependency Injection?
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
Dagger2 --Android Dependency Injection
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?