[JAVA] Spring Autowired is written in the constructor

Overview

Autowired annotations used when doing DI in Spring are easy to use and convenient! However, there are multiple ways to use it, and I am at a loss as to which one to use. I often use it in constructors, but I would like to sort out how that happened.

How to configure Autowired

Rough review. There are three ways to set Autowired.

Field injection

This is a type of declaration method that is written directly in the field.

public class A {
    @Autowired
    private B b;
}

Constructor injection

This is the type of declaration method to write in the constructor.

public class A {
    private final B b;

    @Autowired
    public A(B b) {
        this.b = b;
    }
}

Setter injection

It is a type of declaration method to write in the setter.

public class A {
   private B b;
   
   @Autowired
   public void setB(B b) {
       this.b = b;
   }
}

Why choose constructor injection?

Final can be used for the field to ensure immutability. The reason for this is great. By doing this, it is safe because it will not be rewritten in the middle of processing. (Even people who don't understand in maintenance will get a compile error when rewriting final. Really) Also, it is great that you can make a new test. Personally, I'm happy to be able to choose between DI in the test or just new.

On the contrary, it is not good to use field injection.

In the first place, it is deprecated by the official. (Sure) There would be no point in using it.

I don't trust the sample source to use field injection. I personally think it's no good. (It's really easy)

Summary

Let's use constructor injection without getting tired of any source.

Recommended Posts

Spring Autowired is written in the constructor
What is @Autowired in Spring boot?
[Spring Boot] Until @Autowired is run in the test class [JUnit5]
What is the constructor for?
Event processing is performed in Spring.
Memo after the first Spring project-What is Spring-
What is the main method in Java?
The ruby version is managed in the .rbenv / version file
The intersection type introduced in Java 10 is amazing (?)
How to set when "The constructor Empty () is not visible" occurs in junit
The official name of Spring MVC is Spring Web MVC
[Java] Something is displayed as "-0.0" in the output
@Import target is treated as @Configuration in spring
View the Gradle task in the Spring Boot project
When the project is not displayed in eclipse
Ebean.update () is not executed in the inherited model.
Get multiple Resources that match the pattern in spring
Specify the encoding of static resources in Spring Boot
[Java] How to omit the private constructor in Lombok
In Time.strptime,% j (total date of the year) is
Simple plugin mechanism in Spring, also consider the order
[For beginners] DI ~ The basics of DI and DI in Spring ~
Which is better, Kotlin or Java in the future?
Is there an instance even if the constructor fails?
Make the where clause variable in Spring Data JPA
Let's summarize the reasons why constructor injection is recommended
[Android] Variable of Data Binding is written in camelCase
When Spring Batch is executed continuously in Oracle, ORA-08177
[Error] The app is not displayed in the production environment
Why the get method is needed in the Calendar class
The story that .java is also built in Unity 2018
What is a constructor?
What is Spring Tools 4
Use Interceptor in Spring
Microservices in Spring Cloud
Get cookies in Spring
What you write in C # is written in Ruby like this
Spring Boot 1.x will reach EOL in the next year.
Learn about the spec while shortening FizzBuzz written in Java
Put the file in the properties of string in spring xml configuration
The case that @Autowired could not be used in JUnit5
Determine that the value is a multiple of 〇 in Ruby
Dynamically access the property specified by the string in Spring BeanWrapper
What is the representation of domain knowledge in the [DDD] model?
Error in production environment (The asset "~" is not present in the asset pipeline.)
Behavior when each is executed in the reverse order Range
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.
Throw an exception and catch when there is no handler corresponding to the path in spring