Spring Framework study notes [Part 1] DI container

Introduction

--This time --About DI container, one of the foundations of Spring Framework ――There are many complements based on my interpretation, so if you have any suggestions, please do not hesitate to contact us.

Subject

--The following interfaces and their implementation classes. --User registration work ʻUserService --Permanent or process ʻUserRep --PasswdEnc` to hash passwords ――When you draw a class diagram, it looks like the following.

uml.png

Dependency Injection (DI)

--Suppose the constructors of the ʻUserService class take ʻUserRep and PasswdEnc, their respective implementations as arguments. --At this time, the usage example of the ʻUserServiceImpl` class is as follows.

PasswdEnc passwdEnc = new PasswdEncImpl();
UserRep userRep = new UserRepImpl();
UserService userService = new UserServiceImpl(passwdEnc, userRep);

--In this way, ** setting the components required to initialize a certain class ** is called ** Dependency Injection (DI) . - The infrastructure that automatically performs DI ** is called ** DI container **. ――What does "automatically do" mean? -(I interpret it) * When initializing an instance of a certain interface type, set the implementation you want to call each time in advance to make the description when using the interface easier. *

Bean definition file

――For "preset" ... --Define in the class (** Bean definition file **) with @Configuration annotation. --Add a component with @Bean annotation and specify the implementation you want to call each time. --Such components can also be injected into other components.

@Configuration
public class AppConfig{
  @Bean
  UserRep userRep(){
    return new UserRepImpl();
  }

  @Bean
  PasswdEnc passwdEnc(){
    return new PasswdEncImpl();
  }

  @Bean
  UserService userService(){
    return new UserServiceImpl(userRep(), passwdEnc());
  }
}

--If you set in advance as above, you can easily initialize the ʻUserService` interface type via the DI container.

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); 
//Generate DI container

UserService userService = context.getBean(UserService.class) 
//Get preset implementation via DI container

--In other words, the use of DI containers can be broadly divided into the following three phases.

  1. Register the component in the bean definition
  2. Create a DI container from the bean definition
  3. Call the intended implementation by specifying the DI cottenr and bean (** lookup **)

Component scan

--There is a component scan as a method of bean registration other than the description in the bean definition file. --Add the @Component annotation directly to the implementation class and register it as a bean. If you want to call it from another component, add the @Autowired annotation.

@Component //Register itself as a bean
public class UserRepImpl implemente UserRep {
  //abridgement
}
@Component //Register itself as a bean
public class PasswdEncImpl implemente PasswdEnc {
  //abridgement
}
@Component //While registering itself as a bean...
public class UserServiceImpl implemente UserService {
  @Autowired //Inject other beans
  public UserServiceImpl(UserRep userRep, PasswdEnc passwdEnc)
}

--@Autowired ** searches the DI container for beans ** that match the target type by default. --When performing component scan, it is necessary to specify the package you want to target for component scan in the Bean definition file as shown below.

@Configuration
@ComponentScan("com.example.hoge")
public class AppConfig{
}

--Lookup is the same as when using Bean definition file.

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); 
//Generate DI container

UserService userService = context.getBean(UserService.class) 
//Get preset implementation via DI container

to be continued

References

--NTT DATA Corporation (2016) "Thorough introduction to Spring Java application development with Spring Framework" Shoeisha

Recommended Posts

Spring Framework study notes [Part 1] DI container
Spring Framework Summary-About DI
Major changes related to Spring Framework 5.0 DI container
spring framework Simple study memo (2): AOP
Jigsaw study notes
JSUG Study Group 2018 Part 4 Spring 5 & Spring Boot 2 Hands-on Impressions
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
JavaFX study notes
Play Framework study
Docker study notes
[Java] Study notes
[Memo] JSUG Study Group 2020 Part 1 Spring x Kotlin
[Java] Spring DI ③
Try using DI container with Laravel and Spring Boot
I created an api domain with Spring Framework. Part 2
I created an api domain with Spring Framework. Part 1
[Spring Framework] Configuration split
Spring Framework multilingual support
Spring taglib: form notes
1. Start Spring framework from 1
About DI of Spring ①
study of dbpedia part 25
About DI of Spring ②
Ruby study notes (puts)
Maven basic study notes
play framework personal notes
Spring Basics ~ DI Edition ~
Let's grasp the operation image (atmosphere) of the DI container of Spring
[Memo] JSUG Study Group 2019 Part 7 Utilization of Spring in Visional