--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.
--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.
--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. *
――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.
--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
--NTT DATA Corporation (2016) "Thorough introduction to Spring Java application development with Spring Framework" Shoeisha
Recommended Posts