In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Lombok annotations". In daily operation, I believe many people have doubts about how to use Lombok annotations. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how to use Lombok annotations! Next, please follow the editor to study!
Lombok can eliminate the verbosity of Java, reduce the length of the code, and shift the focus to the place where you should focus. SpringBoot puts Lombok into its dependency, and Java14 even borrows from this idea by introducing the record syntax, which is similar to the following:
Record Point (int x, int y) {}
In this article, I'm not going to discuss anything like the @ Data annotation. Let's discuss a partial note that makes you feel late to meet each other: RequiredArgsConstructor.
Property injection of explosion
Spring provides two injection modes, which are the three ways to write DI that very junior programmers are often asked about. One is attribute injection (Filed injection), one is through the setter method, and the other is constructor injection.
Hoho, I lied. ByName and byType are often asked. These days, however, we often use @ Autowired annotations.
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 fine because the injected fields are limited. But if you haven't seen some project code, you will be fooled by the perfect appearance of the program world.
Business code, uncommented, a single file length of more than 2000 lines can be found everywhere. The performance of the injected genera is as many as a dozen. This part of the injection code is really messy.
Not only that, these fields will also be grayed out in IDE, telling you that the code has become ugly because it has not been initialized.
In fact, Spring has not recommended the use of generic injection patterns since 4. 0, because it allows us to ignore the potential for code to go bad. You can search for this question yourself, and we won't talk about it.
Since Spring recommends using the displayed Setter and constructor methods, let's switch to the implementation.
The Setter method is basically used by fewer people because it is more smelly and longer. If you write a set method for each property, I guess you will throw up even with the code generator.
Constructor injection
In that case, the constructor approach 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 additional annotations to complete the injection using the constructor. The problem is that we still have to write a lot of code.
At this point, you may have thought of Lombok's AllArgsConstructor comments. But it is for all attributes, and if there are some non-Bean attributes in the class, Spring will get dizzy.
At this point, you can use 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 properties that need to be injected to be of type final (or use the @ NotNull annotation, which is not recommended), and these properties will form the default constructor. Java requires that the properties of the final type must be initialized, and the code will turn red if there is no constructor.
We can see the revised IDE, and the annoying gray hints disappear.
This kind of code is very concise.
A little more advanced.
RequiredArgsConstructor comments, you can also write as follows. Even if you change @ _ _ to @ _ _, or change to @ _, it will work normally.
@ RequiredArgsConstructor (onConstructor = @ _ (@ Autowired))
What it means is to add an @ Autowired annotation to the constructor method generated using Lombok. This is an out-and-out Lombok syntax, but now Spring doesn't need such annotations to run.
Look at my following code, it can actually run. Did you enjoy it?
@ RequiredArgsConstructor (onConstructor = @ _ _ (@ Autowired)), the study on "how to use Lombok annotations" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.