I have seen many students who oppose Lombok and secretly add plug-ins. This is the true fragrance principle playing tricks. Say no, physically honest. Those who oppose it should have never seen the lengthy and complicated business code, and still immersed in their own morbid perfectionism.
We should face the dirty and messy working environment and reality.
Lombok can eliminate the verbosity of Java, reduce the length of code, and shift the focus to the place of focus. SpringBoot put Lombok into its dependency. Java 14 even borrowed this idea and introduced the record syntax, which is similar to the following:
record Point(int x, int y) { }
This article is not going to discuss anything like @ Data annotation. Let's discuss a more eccentric note that makes you feel sorry to meet later: required argsconstructor.
Attribute injection of explosion
Spring provides two injection modes, which are also three DI writing methods often asked by very junior programmers. One is file injection, the other is through setter method, and the other is constructor injection.
HOHO, I lied. byName and byType are often asked. However, these days, we often use @ Autowired annotation.
The code is usually written like this.
@Service public class GoodsServiceImpl implements GoodsSrv { @Autowired private GoodsRepo goodsRepo; @Autowired private TagRepo tagRepo; @Autowired private TagRefRepo tagRefRepo; @Autowired private BrandRepo brandRepo; @Autowired private UnitRepo unitRepo; }
This is generally not a problem because the injected fields are limited. But if you haven't seen some project code, you will be deceived by the perfect appearance of the programming world.
Business codes, without comments, can be found everywhere with a single file length of more than 2000 lines. The injected attributes can reach as many as a dozen. This part of the injected code is really messy.
Not only that, these fields will turn gray in the IDE, telling you that they are not initialized and the code becomes ugly.
In fact, Spring has not recommended using attribute * * injection mode * * since 4.0, because it allows us to ignore some hidden dangers of code deterioration. You can search this question by yourself, and we won't talk about it.
Since Spring recommends using the Setter and constructor mode of display, let's switch the implementation scheme.
Setter method is basically used by fewer people because it is more smelly and longer. If you write a set method for each attribute, I guess you'll throw up even if you use the code generator.
Constructor Injection
Then, the constructor method has become our first choice.
The sample code is as follows:
public class GoodsServiceImpl implements GoodsSrv { private GoodsRepo goodsRepo; private TagRepo tagRepo; private TagRefRepo tagRefRepo; private BrandRepo brandRepo; private UnitRepo unitRepo; public GoodsServiceImpl( GoodsRepo goodsRepo, TagRepo tagRepo, TagRefRepo tagRefRepo, BrandRepo brandRepo, UnitRepo unitRepo) { this.goodsRepo = goodsRepo; this.tagRefRepo = tagRefRepo; this.tagRefRepo = tagRefRepo; this.brandRepo = brandRepo; this.unitRepo = unitRepo; this.tagRepo = tagRepo; } }
Spring does not need to add other annotations, so it can use the constructor to complete the injection. The problem is that we still have to write a lot of code.
At this time, you may think of Lombok's all argsconstructor annotation. But it is aimed at all attributes. If there are some non Bean attributes in the class, Spring will faint.
At this time, you can use the RequiredArgsConstructor.
The code is as follows.
@Service @RequiredArgsConstructor public class GoodsServiceImpl implements GoodsSrv { final GoodsRepo goodsRepo; final TagRepo tagRepo; final TagRefRepo tagRefRepo; final BrandRepo brandRepo; final UnitRepo unitRepo; }
We modify the attributes to be injected to final type (or use @ NotNull annotation, not recommended). These attributes will constitute the default constructor. Java requires that attributes of final type must be initialized. If there is no constructor, the code will turn red.
We can see the modified IDE, and the annoying gray prompt disappears.
Such code is very concise.
More advanced
You can also write the required argsconstructor annotation as follows. Even if you change @ to @ or @, it can work normally.
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) Copy code
It means adding an @ Autowired annotation to the constructor method generated by Lombok. This is an out and out Lombok syntax, but now Spring can run without adding such annotations.
Look at the code below. It can actually run. Is it cool?
@RequiredArgsConstructor(onConstructor = @______________________________________( @Autowired ))
What a deadly beauty!
End
In these ways, the number of lines of code you write may drop sharply. In a company that contributes to the number of lines of code, it may help you get 3.25, but this is the pride of 3.25.
These tips, xjjdog are about one less. If you have a friendship for three times, you may be able to get through my second pulse of Ren Du, and you can have less advertising and more dry goods in the future.
Author: little sister taste
Link: https://juejin.im/post/6888985072129540103
Reader welfare
Friends who read this can also get a collection of advanced Java knowledge notes and video materials for free.
Free collection method of materials: after paying attention, click here to receive them for free
Share more notes
B)**
[external chain picture transferring... (img-Ob8OgqB2-1623623478879)]
More notes to share
[external chain picture transferring... (IMG eugseb6y-162362347880)]
[external chain picture transferring... (IMG wkxuus5c-162362347881)]