Throw exception in Spring when loading a file
My first idea was to load the file in the constructor of the class, but I do not know if that is a good practice for Spring.


It takes the lazy-loading out of the lazy-loader, but then you could ask yourself what the remaining uses of the class are.


  As you can see, everytime I have to call to getManifest I throw an IOException.


There are two main ways around this:

1. Use UncheckedIOException to wrap IOException:

    public Properties getManifest() {
      if (p == null) {
        p = new Properties();
        try {
          p.load(servletContext.getResourceAsStream("/META-INF/MANIFEST.MF"));
        } catch (IOException ex) {
          throw new UncheckedIOException(ex);
        }
      }
      return p;
    }


This isn't my preferred approach, because all this does is evade the radar, basically. If you're in a pinch, though, this will do the trick. Mind: UncheckedIOException is not a type of IOException.

(On another note: you probably need to mark p as volatile!)

2. Move the loading to an initialization method:

    public Properties getManifest() {
      return p;
    }

    @PostConstruct
    public void init() throws IOException {
      p = new Properties();
      p.load(servletContext.getResourceAsStream("/META-INF/MANIFEST.MF"));
    }


This approach has the added benefit of being thread-safe: no need to fence access to p, and no risk of having a bean in an inoperable state, or filling the log with errors.