这个问题在这里已经有答案了:
spring@autowire属性vs构造函数(7个答案)
三个月前关门了。
我使用Guava缓存,我想保持最大大小可配置。我尝试使用@value来实现这个,但问题是在@value注入之前创建私有成员缓存。如何从配置属性读取此大小?下面给出了我目前使用的代码
@Component
public class DataProcessor {
@Value("${cacheSize}")
private long cacheSize;
@Value(value = "${rawSensorDataTopic}")
private String rawSensorDataTopic;
private LoadingCache<String, DataPacketGroup> rfPacketsCache = CacheBuilder.newBuilder().maximumSize(cacheSize)
.concurrencyLevel(1).expireAfterWrite(15, TimeUnit.MINUTES)
.build(new CacheLoader<String, DataPacketGroup>() {
@Override
public DataPacketGroup load(String key) throws Exception {
return null;
}
});
1条答案
按热度按时间6l7fqoea1#
可以将缓存创建放在用注解的方法中
@PostConstruct
```@Component
public class DataProcessor {
@Value("${cacheSize}")
private long cacheSize;
@Value(value = "${rawSensorDataTopic}")
private String rawSensorDataTopic;
private LoadingCache<String, DataPacketGroup> rfPacketsCache;
@PostConstruct
private void createcache() {
CacheBuilder.newBuilder().maximumSize(1000)
.concurrencyLevel(1).expireAfterWrite(15, TimeUnit.MINUTES)
.build(new CacheLoader<String, DataPacketGroup>() {
@Override
public DataPacketGroup load(String key) throws Exception {
return null;
}
});
}