Created
July 11, 2020 08:38
-
-
Save siddharthbarman/a160ca2ea995f3f8e50d61cf1a1cb11d to your computer and use it in GitHub Desktop.
Access application properties the classic way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File location: src/main/resources/ | |
app.signature-fetch-url=http://localhost:8082/signatures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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