Java 从实体中的属性文件读取
我的实体中有一个属性expiryDate。我希望在从该实体创建实例时,设置属性expiryDate的值。该值位于application.yml文件中。我在构造中使用了Java 从实体中的属性文件读取,java,spring,spring-boot,Java,Spring,Spring Boot,我的实体中有一个属性expiryDate。我希望在从该实体创建实例时,设置属性expiryDate的值。该值位于application.yml文件中。我在构造中使用了Properties.getProperty,但它不起作用 application.yml: application: token: expiredIn: 1440 令牌实体: public abstract class Token implements Serializable { @Id priv
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");
}
}
#更新
我使用了@Value
,但expiration的值始终为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());
}
}
尝试:
@Value
将从application.yml
文件中获取值,并将其注入expiryIn
您确定它是正确的路径吗?我将使用@Value(${myProperties.property}”)
注释来实现这一点。
public abstract class Token implements Serializable {
@Id
private UUID id;
@Value("${application.token.expiredIn}")
private int expiryIn;
}