The task is to subtract the key from the config and, if necessary, use the key from the memory, rather than constantly deducting and wasting resources

The problem is that it doesn’t work out with a singleton constructor of a class (public LoggerImpl (PropertiesLoader propertiesLoader)) that reads the line with the key "accessToken" from the config file.

Tell me, please, how can Singletto implement a one-time file read?

package org.BrovaryCityCouncil.logging; import com.rollbar.Rollbar; import org.BrovaryCityCouncil.properties.PropertiesLoader; public class LoggerImpl implements Logger { private PropertiesLoader propertiesLoader; private Rollbar rollbar; private static LoggerImpl instance; public LoggerImpl() { super(); } public synchronized LoggerImpl getInstance(){ if (instance == null){ instance = new LoggerImpl(propertiesLoader); } return instance; } public LoggerImpl (PropertiesLoader propertiesLoader) { this.propertiesLoader = propertiesLoader; // считываем из файла конфига rollbar = new Rollbar(propertiesLoader.getProperty("accessToken"), "production"); } public void logWarning(Throwable error) { rollbar.warning(error); } public void logCritical(Throwable error) { rollbar.critical(error); } public void logInfo(Throwable error) { rollbar.info(error); } public void logDebug(Throwable error) { rollbar.debug(error); } public void logger(Throwable error) { rollbar.log(error); } public void logError(Throwable error) { rollbar.error(error); } public Rollbar getRollbar() throws Exception { return rollbar; } } 

Сlass PropertiesLoaderImpl

 public class PropertiesLoaderImpl implements PropertiesLoader { @Override public String getProperty(String propertyName) { Properties properties = new Properties(); try { FileInputStream file = new FileInputStream("src/main/resources/config.properties"); properties.load(file); return properties.getProperty(propertyName); } catch (IOException e) { e.printStackTrace(); } return null; } } 
  • do you use spring? - Senior Pomidor
  • one
    Remove public LoggerImpl() , and public LoggerImpl(PropertiesLoader propertiesLoader) make private . Creating a LoggerImpl from the outside in this case will not be possible. - Regent
  • And, yes: the getInstance method must be static . - Regent
  • without spring. Regent thanks, now everything seems to be ok - GoodWin

0