【问题标题】:Lombok with default value from config龙目岛,配置中的默认值
【发布时间】:2020-04-16 12:06:08
【问题描述】:

我有这个 Lombok 类,但是当我创建类时没有注入值

@Data
@Builder
@Component
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
@EqualsAndHashCode
public class Task  {


    @org.springframework.beans.factory.annotation.Value("${timeInMinutes}")
    private @NotNull Integer timeInMinutes;

【问题讨论】:

  • 这是哪个Value注解?
  • 你是如何创建这个类的?该注释看起来像一个 Spring 注释。 Spring 只能为它管理的 bean 注入这些值(即,如果你自己从代码中调用构造函数,它是一个非托管 bean,spring 不会做任何事情)
  • @Chris 这个类有组件注释,所以这不是问题。问题在于 AllArgsConsructor

标签: java spring-boot lombok


【解决方案1】:

如果您想从应用程序属性中获取值,请使用自定义构造函数而不是使用注解 @AllArgsConstructor。

public class Task {


  private Integer timeInMinutes;

  Task(@Value("${timeInMinutes}") Integer timeInMinutes){
       this.timeInMinutes = timeInMinutes;
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 2021-10-02
    • 2021-03-27
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2011-06-19
    相关资源
    最近更新 更多