Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

lombok - Spring: Properly use of lombock Builder

I faced with problem while using of lombock @Builder.

In SpringBoot application I create the following component:

@Getter
@Builder
@Component
@AllArgsConstructor
public class ContentDTO {
    private UUID uuid;
    private ContentAction contentAction;
    private String payload;
}

But when I run the application< I receive:

 Error creating bean with name 'contentDTO': Unsatisfied dependency expressed through constructor parameter 0

Caused by:

 No qualifying bean of type 'java.util.UUID' available: expected at least 1 bean which qualifies as autowire candidate

"Finger to the sky", I changed lombock-builder to custom builder, like this:

 @Getter
 @Component
public class ContentDTO {

  private ContentDTO() {       
  }

 // fields

  public static Builder newBuilder() {
       return new ContentDTO().new Builder();
  }

 public class Builder{
    private Builder() {
        private constructor
    }


  public ContentDTO build() {
       return ContentDTO.this;
      }     
   }
}

And problem is gone.

Its nice, but I clearly dont understand, what was problem!

Why in this case lombock-builder prevented the autowiring of beans?

And how to use lombock-builder properly in Spring ApplicationContext?

question from:https://stackoverflow.com/questions/65829596/spring-properly-use-of-lombock-builder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the use of the builder requires a default constructor. When you added the @AllArgsConstructor annotation the problem appears. therefore, you must also add the @NoArgsConstructor annotation. That should be the solution for your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...