[JAVA] About DI of Spring ①

Purpose of this article

DI: What is Dependency Injection? Continued Explanation of how Spring realizes DI

Scope of this article

Outline explanation of DI in Spring

DI review

--Reason for using DI ... I want to reduce the dependency between classes and improve reusability / testability. --How to realize ... Pass the class you want to use in a certain class with the interface type → Define the class to be actually passed separately.

DI概略.jpg

Character

--Bean ... Injection class --Bean definition ... Various properties set in the bean when injecting the bean --Bean definition file / Configuration ... A collection of Bean definitions (Java class, XML file) --DI container ... Set various beans and pass them to the other party when using them.

Outline of DI realization in Spring

Next time, detailed movements of each class ApplicationContext -** The central figure in realizing DI in Java ** --A class that has the function of a DI container --A class created by extending the BeanFactory class --There is a class that is further extended by the corresponding Configuration etc. --Multiple DI containers can be created for one application

Configuration --The Bean definition file --Pass this to the ApplicationContext class and generate a bean in the DI container -There are three methods: [Java-based], [xml-based], and [annotation-based]. ――Three can be used alone, but setting becomes easier by using the annotation base and others in combination. --Write the Bean definition in this

Bean --General class, basically no special description is required If --is an annotation-based Configuration, write an annotation separately. --You can also inject and nest beans inside the bean

ex) Describe the initialization condition for the bean of HogeService in hogeConfig and get the bean via ApplicationContext (Java base)

CreateContext.java


ApplicationContext ctx = new AnnotationConfigApplicationContext(HogeConfig.class);
HogeService service = ctx.getBean(HogeService.class)

HogeConfig.java


@Configuration
public class HogeConfig {
	@Bean
	HogeService hogeService() {
		return new HogeService(); 
	}
}

Is it like the following when expressed in a conceptual diagram? SpringにおけるDIの概要.jpg

I think it's relatively easy to understand if you swallow the concept.

Next time preview

We are planning to explain the specifications etc. when actually trying it.

Recommended Posts

About DI of Spring ①
About DI of Spring ②
About Spring ③
About Spring DI related annotations
About binding of Spring AOP Annotation
About spring AOP
[Java] Spring DI ③
About the initial display of Spring Framework
[Spring] Pitfalls of BeanUtils.copyProperties
About disconnect () of HttpURLConnection
Summary of what I learned about Spring Boot
About the official start guide of Spring Framework
About Spring Security authentication
Spring Framework Summary-About DI
About Bean and DI
About selection of OpenJDK
First Spring Boot (DI)
About Spring AOP Pointcut
About form. ○○ of form_with
About @Accessors of Lombok
Overview of Spring AOP
Spring Basics ~ DI Edition ~
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Spring boot] I thought about testable code by DI
About the handling of Null
[Personal memo] About Spring framework
About simple operation of Docker
About Spring Framework context error
About the description of Docker-compose.yml
About size comparison of compareTo
About types of code coverage
Introduction to Spring Boot ① ~ DI ~
Memorandum of understanding about LOD.
[Java] How Spring DI works
About partial match of selector
Accelerate testing of Validators that require DI in Spring Boot
Let's grasp the operation image (atmosphere) of the DI container of Spring
About the behavior of ruby Hash # ==
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Spring Framework 5.0 Summary of major changes
About the basics of Android development
About fastqc of Biocontainers and Java
About Lambda, Stream, LocalDate of Java8
About error handling of comment function
[Rails] About implementation of like function
Going out of message (Spring boot)
[Spring Boot] Role of each class
About the role of the initialize method
[Java] Spring DI ④ --Life cycle management
About removeAll and retainAll of ArrayList
Think about the 7 rules of Optional
About =
About image upload of jsp (servlet)
About Disk Cache of Glide 4 series
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About error when implementing spring validation
Filter the result of BindingResult [Spring]
Summary about the introduction of Device
About the log level of java.util.logging.Logger
Personal memo Features of Spring Boot (mainly from a DI point of view)