title: Is it allowed to have instance variables in Spring Services?
body: <p>I have a spring service that provides configuration data. When the service is invoked by the GUI, it loads the configuration data from the database. It turns out that this happens quite often during the rendering of a single request. I want to optimize this by caching the configuration data. However, I am not sure if this is a good programming style or if it is "allowed" to have an instance variable in a service.</p>
  
  <p>Here is some example code of what I am thinking of doing:</p>
  
  <pre><code>@Serivce("MyConfigService")
  public class MyConfigServiceImpl implements MyConfigService {
  
      private Config cachedConfig;
  
      @Override
      public Config loadConfig() {
          if (cachedConfig != null) {
              // load config
              cachedConfig = loadedConfig;
          }
          return cachedConfig;
      }
  
      @Override
      public saveConfig(Config config) {
          cachedConfig = null;
          // save the configuration
      }
  }
  </code></pre>