从实体

时间:2018-06-11 13:39:18

标签: java spring spring-boot

我的实体中有一个属性expiryDate。我想当我从这个实体中停止一个实例时,我设置了属性expiryDate的值。该值位于application.yml文件中。我在构造中使用了Properties.getProperty,但它没有工作

application.yml:

application:
  token:
    expiredIn: 1440

令牌实体:

public abstract class Token implements Serializable {

    @Id
    private UUID id;

    private int expiryIn;

    public Token() {
        this.expiryIn= Properties.getProperty("application.token.expiredIn");
    }

}

#UPDATE

我使用@Value但是到期值始终为0

public abstract class Token implements Serializable {

    @Id
    private String id;

    private Date expiryDate;

    @Value("${application.token.expiredIn}")
    private static int expiration;

    public Token() {
        this.expiryDate = calculateExpiryDate(expiration);
    }

    private Date calculateExpiryDate(final int expiryTimeInMinutes) {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(new Date().getTime());
        cal.add(Calendar.MINUTE, expiryTimeInMinutes);
        return new Date(cal.getTime().getTime());
    }

}

1 个答案:

答案 0 :(得分:1)

尝试:

   public abstract class Token implements Serializable {

    @Id
    private UUID id;

    @Value("${application.token.expiredIn}")
    private int expiryIn;


}

@Value将获取application.yml文件中的值并将其注入expiryIn