Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...