The_Unknown
The_Unknown

Reputation: 150

Bug with @AllArgsConstructor in Lombok?

I have the following error when starting my spring boot application:

Parameter 17 of constructor in be.mypackage.dataimport.soft99.transformers.visit.VisitTransformer required a bean of type 'java.lang.String' that could not be found.

I am using @AllArgsConstructor on VisitTransformer but I also have a private String variable for that class defined. It's just a String so now he assumes it should be a bean for the constructor and gives the above error.

This code used to work but as of last friday 2023-10-6 doesn't anymore. I looked at the changelog of lombok but didn't find anything specific related to this so I'm wondering whether they unknowingly introduced a bug?

(P.S.: The fix is to just not use @AllArgsConstructor anymore and define the constructor yourself.)

Upvotes: 0

Views: 1939

Answers (1)

Andrei Lisa
Andrei Lisa

Reputation: 4906

According to java docs of annotation(@AllArgsConstructor) it create a constructor for all class fields

Generates an all-args constructor. An all-args constructor requires one argument for every field in the class.

Lombok

@AllArgsConstructor
public class Employee {

    private String name;
    private int age;
}

Delombok

public class Employee {

    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age= age;
    }
}

And I think this Parameter 17 of constructor was added by you and now you can not create the @Service or @Component. Usually in this case you can use Lombok but with another annotation @RequiredArgsConstructor(it is usually used when create a constructor injection in Spring Component without create a plain constructor).

Generates a constructor with required arguments. Required arguments are final fields and fields with constraints such as @NonNull.

Lombok

@RequiredArgsConstructor
public class Employee {

    private final String name;
    private int age;
}

Delombok

public class Employee {

    private final String name;
    private int age;

    public Employee(String name) {
        this.name = name;
    }
}

It is going to help you. also why to use @RequiredArgsConstructor because usually all fields into Spring Component that is another Spring Component is declared as private final

Upvotes: 2

Related Questions