如何在注释中使用 Spring 变量?
How Can I use a Spring Variable Inside of an annotation?
如何在不在 class 范围内的注释中获取我的 spring 配置变量之一?目前,我正在这样做:
@Data
@RedisHash(value = "MyEntity", timeToLive = 604800 )
public class MyEntity{
private String id;
private String name;
}
但我真正想做的是这个(或类似的东西):
@Data
@RedisHash(value = "MyEntity", timeToLive = @Value("${spring.redis.expire}") )
public class MyEntity{
private String id;
private String name;
}
任何允许我在我的注释中访问我的 application.yml 中的配置变量的解决方案(在这种情况下,完成我的 @RedisHash 注释的值)???
我想这应该可行。使用不带@Value 注释的 属性。
@RedisHash(value = "MyEntity", timeToLive = "${spring.redis.expire}" )
public class MyEntity{
private String id;
private String name;
}
事实证明,您可以通过在 @RedisHash
注释实体 class 中使用 @TimeToLive
注释来分配可配置的过期值。就我而言,它看起来像这样:
@Data
@RedisHash(value = "MyEntity" )
public class MyEntity{
private String id;
private String name;
@TimeToLive
private Long expiration;
}
然后在使用 redis 实体的实现中,您只需分配该过期值 [myEntity.setExpiration(expiration);
],如以下代码所示:
@Service
@RequiredArgsConstructor
@Slf4j
public class MyEntities {
private final EntityRepository entityRepository;
private boolean redisRepoIsHealthy = true;
@Value("${spring.redis.expire}")
private long expiration;
. . . . .
. . . . .
. . . . .
. . . . .
private MyEntity saveEntityToRedisRepo(String userId, String name) {
MyEntity myEntity = null;
try {
myEntity = new MyEntity();
myEntity.setId(userId);
myEntity.setAuthorities(name);
myEntity.setExpiration(expiration);
entityRepository.save(myEntity);
}
} catch (RuntimeException ex) {
// user is already saved in redis, so just swallow this failure
if (redisRepoIsHealthy) {
log.info("User {} already exists. Could not be saved.", userId);
log.info("An error occurred ", ex);
} else {
log.info("The redis repo is corrupt. The user {} could not be saved.", userId);
}
}
return myEntity;
}
如何在不在 class 范围内的注释中获取我的 spring 配置变量之一?目前,我正在这样做:
@Data
@RedisHash(value = "MyEntity", timeToLive = 604800 )
public class MyEntity{
private String id;
private String name;
}
但我真正想做的是这个(或类似的东西):
@Data
@RedisHash(value = "MyEntity", timeToLive = @Value("${spring.redis.expire}") )
public class MyEntity{
private String id;
private String name;
}
任何允许我在我的注释中访问我的 application.yml 中的配置变量的解决方案(在这种情况下,完成我的 @RedisHash 注释的值)???
我想这应该可行。使用不带@Value 注释的 属性。
@RedisHash(value = "MyEntity", timeToLive = "${spring.redis.expire}" )
public class MyEntity{
private String id;
private String name;
}
事实证明,您可以通过在 @RedisHash
注释实体 class 中使用 @TimeToLive
注释来分配可配置的过期值。就我而言,它看起来像这样:
@Data
@RedisHash(value = "MyEntity" )
public class MyEntity{
private String id;
private String name;
@TimeToLive
private Long expiration;
}
然后在使用 redis 实体的实现中,您只需分配该过期值 [myEntity.setExpiration(expiration);
],如以下代码所示:
@Service
@RequiredArgsConstructor
@Slf4j
public class MyEntities {
private final EntityRepository entityRepository;
private boolean redisRepoIsHealthy = true;
@Value("${spring.redis.expire}")
private long expiration;
. . . . .
. . . . .
. . . . .
. . . . .
private MyEntity saveEntityToRedisRepo(String userId, String name) {
MyEntity myEntity = null;
try {
myEntity = new MyEntity();
myEntity.setId(userId);
myEntity.setAuthorities(name);
myEntity.setExpiration(expiration);
entityRepository.save(myEntity);
}
} catch (RuntimeException ex) {
// user is already saved in redis, so just swallow this failure
if (redisRepoIsHealthy) {
log.info("User {} already exists. Could not be saved.", userId);
log.info("An error occurred ", ex);
} else {
log.info("The redis repo is corrupt. The user {} could not be saved.", userId);
}
}
return myEntity;
}