How to set Dependency Injection (DI) for Spring Boot

We have summarized how to set Dependency Injection for Spring Framework.

Specifying the scan target of the component

By default, packages in the same hierarchy as the package of the class with @ComponentScan are scanned. If you want to scan other classes, specify the package name in scanBasePackage.

// @It can also be specified in compound annotations such as SpringBootApplication.
@ComponentScan(scanBasePackages={"com.example"})

Dependency injection (DI) settings

When using Spring annotation

Define the dependent object as a member of private`` final, and define the constructor that receives the dependent object as an argument in the constructor. When using Lombok, it can be described as follows.

Component definition
@Component
class ComponentA { ...
DI settings
@RequiredArgsConstructor
class ClassA {
	// @Autowired can be omitted.
	private final ComponentA componentA;
	...

Previously, it was mainstream to specify @Autowired for non-final members or their Setters.

class ClassA {
	@Autowired
    private ComponentA componentA;
	...
class ClassA {
	private ComponentA componentA;
	@Autowired
	public void setComponentA(ComponentA componenA) {
		...

This is currently recommended because using the constructor has the following advantages:

--Become a module that can be used without DI. (The dependency can be set in the constructor on the user side.) --You can add the final qualifier. (The variable that stores the DI object is not usually rewritten.)

When using JSR-330 annotation

Since it is a Java standard, it is easy to switch to another DI container.

Component definition
@Named //Or@ManagedBean
class ComponentA { ... }
DI settings

Specify @Inject in the constructor (or Setter). (Setter is also acceptable.)

@RequiredArgsConstructor(onConstructor=@__(@Inject))
class ClassA {
	private final ComponentA componentA;
	...

Alternatively, specify @Inject as a member.

class ClassA {
	@Inject
	private ComponentA componentA;
    ...

reference

Recommended Posts

How to set Dependency Injection (DI) for Spring Boot
How to set Spring Boot + PostgreSQL
Spring Boot --How to set session timeout time
Introduction to Spring Boot ① ~ DI ~
How to write a unit test for Spring Boot 2
[Spring Boot] How to create a project (for beginners)
How to use ModelMapper (Spring boot)
[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to split Spring Boot message file
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to add a classpath in Spring Boot
[Java] (for MacOS) How to set the classpath
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
First Spring Boot (DI)
How to set environment variables in the properties file of Spring boot application
Plans to support JDK 11 for Eclipse and Spring Boot
Settings for connecting to MySQL with Spring Boot + Spring JDBC
[Java] How to omit spring constructor injection with Lombok
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to boot by environment with Spring Boot of Maven
What is DI (Dependency Injection)
Set context-param in Spring Boot
DI: What is Dependency Injection?
How to set Docker nginx
Introduction to Spring Boot ② ~ AOP ~
How to set Java constants
Set cookies with Spring Boot
[Java] How Spring DI works
Introduction to Spring Boot Part 1
Spring Boot for annotation learning
Steps required to issue an asynchronous event for Spring Boot
Change the injection target for each environment with Spring Boot 2
How to change application.properties settings at boot time in Spring boot
How to call and use API in Java (Spring Boot)
How to control transactions in Spring Boot without using @Transactional
How to use Lombok in Spring
How to unit test Spring AOP
Spring Boot for the first time
How to specify validation for time_field
How to install JMeter for Mac
How to use Spring Data JDBC
[Spring Boot] What to do if an HttpMediaTypeNotAcceptableException occurs on an endpoint for which produces is set
[How to install Spring Data Jpa]
Dependency Injection to understand step by step
How to create your own Controller corresponding to / error with Spring Boot
How to set and use profile in annotation-based Configuration in Spring framework
Frequent annotations for Spring Boot tests
Use DBUnit for Spring Boot test
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
Upgrade spring boot from 1.5 series to 2.0 series
How to set Lombok in Eclipse
How to display characters entered in Spring Boot on a browser and reference links [Introduction to Spring Boot / For beginners]
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
Spring.messages.fallback-to-system-locale: false is required to default message.properties for i18n support in Spring boot