У меня есть приложение для фреймворка. Я пытаюсь получить доступ к кешу через статический метод. Я решил обернуть кеш в singleton, но я получаю NullPointerException, когда пытаюсь получить доступ к переменной кэша в классе CacheSingleton. Как проблема может быть решена? Благодарю.Play Framework CacheApi injection return NullPointeerException в singleletone
import javax.inject.*;
import play.cache.*;
@Singleton
public final class CacheSingleton {
@Inject CacheApi cache;
private static volatile CacheSingleton instance = null;
private CacheSingleton() {
}
public static CacheSingleton getInstance() {
if (instance == null) {
synchronized(CacheSingleton.class) {
if (instance == null) {
instance = new CacheSingleton();
}
}
}
return instance;
}
}
public class CustomLabels {
public static String get() {
CacheSingleton tmp = CacheSingleton.getInstance();
try
{
tmp.cache.set("key", "value");
}catch(Exception e){}
}
}