Skip to content

Instantly share code, notes, and snippets.

@siddharthbarman
Created July 11, 2020 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siddharthbarman/a160ca2ea995f3f8e50d61cf1a1cb11d to your computer and use it in GitHub Desktop.
Save siddharthbarman/a160ca2ea995f3f8e50d61cf1a1cb11d to your computer and use it in GitHub Desktop.
Access application properties the classic way
package com.sbytestream.sample;
public class Application {
public static void main(String[] args) {
try {
ApplicationProperties applicationProperties = new ApplicationProperties();
System.out.println(applicationProperties.getSignatureFetchUrl());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
# File location: src/main/resources/
app.signature-fetch-url=http://localhost:8082/signatures
package com.sbytestream.sample;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ApplicationProperties {
public ApplicationProperties() throws IOException {
load();
}
private void load() throws IOException {
Properties properties = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
if (is != null) {
properties.load(is);
}
else {
throw new FileNotFoundException(PROPERTIES_FILE + " not found");
}
signatureFetchUrl = properties.getProperty("app.signature-fetch-url");
is.close();
}
public String getSignatureFetchUrl() {
return signatureFetchUrl;
}
private String signatureFetchUrl;
private final String PROPERTIES_FILE = "application.properties";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment