Original: yuanyang not envy immortal, a line of code tune half a day. Little sister taste (wechat public ID: XJJdog), welcome to share, reprint please keep the source.

I’ve seen a lot of people who are against Lombok add plugins behind their backs, and it’s just the maka principle. The mouth says no, the body is honest. The people who object are those who have not seen the complexity of some business code and are immersed in their own pathological perfectionism.

To face the dirty and messy working environment, face reality.

Lombok removes the rigidities of Java, reduces the length of the code, and shifts the focus to where it needs to be. SpringBoot puts Lombok in its dependencies, and Java14 even borrows from this idea, creating a record syntax that looks something like this:

record Point(int x, int y) {}Copy the code

In this article, I’m not going to discuss things like @data annotations. Let’s talk about a slightly off-kilter, but one that you might have seen too late: RequiredArgsConstructor.

Explosion attribute injection

Spring provides two injection modes, and these are the three ways to write DI that very junior programmers are often asked about. One is field injection, one is through setter method, and the other is constructor injection.

I’m lying, Joho. I’m often asked byName and byType. However, these days, we use more with @autowired annotations.

Code usually looks 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;
}
Copy the code

This is generally fine because the number of fields injected is limited. But if you haven’t seen some of the project code, you may be fooled by this appearance of procedural perfection.

Business code, without comment, single file length more than 2000 lines everywhere. As many as a dozen properties can be injected. This part of the injection code is really messy.

Not only that, but these fields are greened in the IDE, telling you that they are uninitialized, making the code ugly.

In fact, since Spring 4.0, the attribute injection mode has been deprecated because it allows us to ignore the potential for code to go bad. You can search the question yourself, and we won’t go into it.

Since Spring recommends using the displayed Setter and constructor approach, let’s switch the implementation.

The Setter method is basically less used because it’s more smelly and longer. Write a set method for each property and you’ll probably be sick of using a code generator.

Constructor injection

So, the constructor method is our first choice.

The example 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; }}Copy the code

Spring uses the constructor to do the injection without adding additional annotations. The problem is, we still have to write a lot of code.

At this point, you might think of Lombok’s AllArgsConstructor annotation. But it’s for all properties, and Spring gets confused if there are non-bean properties in the class.

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;
}
Copy the code

We modify the properties we want to inject to be final (or use the @notnull annotation, which is not recommended). These properties will form the default constructor. Java requires that properties of final types must be initialized, and the code turns red if there is no constructor.

You can see the modified IDE, and the annoying gray prompt is gone.

Code like this, it’s pretty neat.

A little more advanced

The RequiredArgsConstructor annotation, you can also write as follows. Even if @__ is replaced with @_, or @___, it will work.

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
Copy the code

This means adding an @AutoWired annotation to the constructor method generated using Lombok. This is Lombok syntax through and through, but Spring now runs without such annotations.

Look at my code below, it actually works. Great not great?

@RequiredArgsConstructor(onConstructor = @______________________________________( @Autowired ))
Copy the code

What a bloody beauty!

End

In these ways, the number of lines of code you write may drop dramatically. At companies that contribute to line of code theory, you might get 3.25, but that’s a lot to be proud of.

These tips, XJjdog is about one less one, if you come to a friendship three, maybe I can get through the two channels, the future can be less advertising more dry goods.

Author introduction: Little sister taste (XJjdog), a programmer is not allowed to detour the public number. Focus on infrastructure and Linux. Ten years of architecture, 10 billion traffic per day, and you discuss the high concurrency world, give you a different taste. My personal wechat xJJdog0, welcome to add friends, further communication.