[Java] How to omit spring constructor injection with Lombok

Make a note of the technique used by seniors at work

Postscript: All were posted on this blog http://pppurple.hatenablog.com/entry/2016/12/29/233141

1. What is Lombok?

This is a convenient library that automatically generates boilerplate code (a standard code that cannot be omitted due to language specifications) at compile time. For example, JavaBean getters / setters can be annotated.

2. Try constructor injection

Suppose HogeLogic is using FugaLogic and you want to get it from a DI container.

2-1. General Spring constructor injection

Let's write a normal Spring constructor injection.

Sample A

public class HogeLogic {

  private final FugaLogic fugaLogic;

  @Autowired // <- 4.Can be omitted after 3
  public HogeLogic(FugaLogic fugaLogic) {
    this.fugaLogic = fugaLogic;

  // some method

The important thing is that since ** spring4.3 you can omit @Autowired if you have one constructor **.

2-2. Constructor injection omitted in Lombok

Let's write the constructor injection omitted in Lombok. Just annotate @RequiredArgsConstructor to the class: angel:

Sample B

@RequiredArgsConstructor // <-here
public class HogeLogic {

  private final FugaLogic fugaLogic;

  // some method

Tips. Try delombok

Looking at the automatically generated code, it looks like this:

Sample C

public class HogeLogic {

  private final FugaLogic fugaLogic;

  public HogeLogic(FugaLogic fugaLogic) {
    this.fugaLogic = fugaLogic;

  // some method

The @Autowired pear version of Sample A is complete.

3. What do you mean?

@RequiredArgsConstructor Annotation to generate a constructor that has initialization parameters of fields that need to be initialized (such as final fields) as arguments. [TERASOLUNA Server Framework for Java (5.x) Development Guideline | 11.2. Elimination of Boilerplate Code (Lombok)](http://terasolunaorg.github.io/guideline/5.3.1.RELEASE/en/Appendix/Lombok .html)

The effect of @RequiredArgsConstructor creates a constructor, Since there is only one constructor, it means that @Autowired is omitted. The constructor was automatically injected ...

The combined technique is amazing.

Recommended Posts

[Java] How to omit spring constructor injection with Lombok
[Java] How to omit the private constructor in Lombok
How to use Lombok in Spring
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
Studying how to use the constructor (java)
[Java] How to test for null with JUnit
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
[Java] Article to add validation with Spring Boot 2.3.1.
How to call functions in bulk with Java reflection
How to set Dependency Injection (DI) for Spring Boot
How to deploy Java to AWS Lambda with Serverless Framework
[Java] How to encrypt with AES encryption with standard library
How to build Java development environment with VS Code
How to boot by environment with Spring Boot of Maven
[Java] How to start a new line with StringBuilder
[Java] How to use Map
Java Config with Spring MVC
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to write java comments
How to use @Builder (Lombok)
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to number (number) with html.erb
How to use Java Map
How to update with activerecord-import
How to set Java constants
Connect to DB with Java
Connect to MySQL 8 with Java
Using Mapper with Java (Spring)
[Java] How Spring DI works
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to use Lombok now
How to initialize Java array
How to decompile apk file to java source code with MAC
How to use trained model of tensorflow2.0 with Kotlin / Java
How to handle exceptions coolly with Java 8 Stream or Optional
How to call and use API in Java (Spring Boot)
Investigated how to call services with Watson SDK for Java
How to scroll horizontally with ScrollView
How to study Java Silver SE 8
How to use Java HttpClient (Get)
NoSuchMethodException with lombok without default constructor
How to execute Postgresql copy command with column information on Java
How to unit test Spring AOP
Java to learn with ramen [Part 1]