[JAVA] About DI of Spring ②

Purpose of this article

About Spring DI ① Is a continuation of

Scope of this article

Mainly deals with Configuration

What is Configuration?

** Bean definition file **

function

--It acts as a bridge between Bean and DI container, which defines [which instance] and [which initial value] to pass to DI container. --May be close to the mapping file in ORM ――It can be said that it is the [external] itself of the basic function of DI, which is [having object information externally]. --Maintenance is easier by putting the state of objects together in these files instead of putting them separately in the source code.

Configurationの概略.jpg

type

--There are four types: Java classes, XML files, and two combinations of annotations.

** Java-based Configuration **

JavaConfig.java


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HogeConfig {
	@Bean
	HogeService hogeService() {
		return new HogeService():
	}
	
	@Bean
	AAABean aAABean() {
		return new AAABean(hogeService());
	}
	
	@Bean(name = "bBean")
	BBBBean bBBBean() {
		return new bBBBean();
	}
}

--Explicitly add @Configuration annotation to Configuration class --Multiple annotations can be added --You can refer to other beans by adding arguments to each method. --You can also explicitly define the bean name (name = "hoge") Try it when getting an instance from a DI container

** XML-based Configuration **

xmlConfig.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http;//www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="hogeService" class="jp.co.hogeProgram.HogeService" />
    
    <bean id="aAABean" class="jp.co.hogeProgram.AAABean">
        <constructor-arg ref="hogeService"/>
    </bean>

    <bean id="bBean" class="jp.co.hogeProgram.BBBBean>
        <constructor-arg value="aaa"/>
    </bean>
</beans>

--Define a bean inside the bean element --You can also pass other beans and scalar values with constoructor-arg

Since it is necessary to write the definition for each of the above two as many as the number of beans, it takes time and effort, so it is common to perform DI by combining the following annotation bases.

** Annotation-based Configuration **

Bean

Hoge.java


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Hoge {
	
Auto wiring
By describing this annotation, FugaService will automatically fetch the bean from the DI container.
	@Autowired
	public Hoge(FugaService fuga);
	
Describe the process
}

Config class (xml omitted) In this case, look for a class under jp.co.hoge

AppCongig.java


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("jp.co.hoge")
public class AppConfig {

}

--The class with @Component annotation can be automatically set in the DI container by "Component scan". This is called "auto wiring"

--There are multiple annotations depending on the role of the class such as @Service as well as @Component, but it is basically the same as @Component.

--To enable component scan, @ComponentScan ("package") in the Config class or XML, or describe the package in the context: component-scan element and look for the annotated class that exists under the package.

--The wider the scan range package, the longer it will take, so you need to set an appropriate range.

Next time preview

I plan to do bean association

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
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 ~
About the handling of Null
[Personal memo] About Spring framework
About an instance of java
About simple operation of Docker
About Spring Framework context error
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
About the function of Spring Boot due to different versions
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
[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)