按照纪尧姆在问题评论部分的建议,这是我实施的解决方案。
@Component
public class FooClass implements InitializingBean {
private static final String BUCKET_NAME = "bucket-name";
private static final String FILE_PATH = "path/filename.extension";
private static byte[] instanceVariable;
@Autowired
private Storage storage;
public byte[] get() {
return instanceVariable;
}
// Replacement of @PostConstruct
@Override
public void afterPropertiesSet() {
setInstanceVariable();
}
@Scheduled(fixedRate = 60 * 1000) // Refresh each minute
private void refreshAssociationRule() {
setInstanceVariable();
}
private void setInstanceVariable() {
Blob blob = storage.get(BUCKET_NAME, FILE_PATH);
instanceVariable = blob.getContent();
}
}
另一个 Spring 组件可以简单地自动装配FooClass
并获取资源。
@Service
public class BarClass {
@Autowired
private FooClass fooClass;
private void func() {
byte[] resource = fooClass.get();
// use resource here
}
}
注意:如果资源不是像 String 这样的“传递引用值”数据类型,这可能会导致并行请求中的内存膨胀。由于每个get()
函数调用都会为内容分配内存资源,而不仅仅是引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句