Config
/**
* Loading a properties file (best practice)
*
* Configuration files should be placed in:
* src/main/resources
*
* Example:
* src/main/resources/config.properties
*
* Maven/Gradle automatically puts everything in /resources
* onto the application's CLASSPATH - even inside a JAR.
*
* Classpath loading works everywhere, we can get it using:
* Config.class.getClassLoader().getResourceAsStream()
*/
package com.minte9.collections.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
public static void main(String[] args) throws IOException {
InputStream input = Config.class
.getClassLoader()
.getResourceAsStream("config.properties");
if (input == null) {
throw new RuntimeException("config.properties not found");
}
Properties props = new Properties();
props.load(input);
props.list(System.out);
/**
* db.user=myuser
* db.pass=mypassword
* db.port=9000
* db.url=localhost
*/
System.out.println(props.getProperty("db.user")); // myuser
System.out.println(props.getProperty("db.port")); // 9000
}
}
MultiConfig
/**
* Loading multiple configuration files.
*
* Folder (classpath):
* src/main/resources/
* database-dev.properties
* database-prod.properties
*/
package com.minte9.collections.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class MultiConfig {
public static void main(String[] args) throws IOException {
// Determine environment (dev / prod)
String env = System.getProperty("env", "dev"); // default 'dev'
System.out.println("Environment: " + env); // dev
// Load properties
Properties props = new Properties();
loadProps(props, "database-" + env + ".properties");
// Use properties
System.out.println(props.getProperty("db.url")); // localhost
System.out.println(props.getProperty("db.user")); // dev_user
}
private static void loadProps(Properties props, String filename) throws IOException {
InputStream input = MultiConfig.class
.getClassLoader()
.getResourceAsStream(filename);
if (input == null) {
throw new RuntimeException("Propertis file not found: " + filename);
}
props.load(input);
}
}
Questions and answers:
Clink on Option to Answer
1. Where should configuration files be placed in a Java project?
- a) src/main/resources
- b) src/main/java
2. What does Maven/Gradle do with everything inside /resources?
- a) Ignores it
- b) Puts it on the application’s classpath
3. What does getResourceAsStream() do?
- a) Loads a file from the classpath
- b) Loads a file only from the file system
4. If a config file is missing, what should your program do?
- a) Throw an error
- b) Silently continue
5. In the multi-config example, how is the environment chosen (dev/prod)?
- a) Using a system property (System.getProperty("env"))
- b) Hard-coded inside the code
6. What happens if you call props.getProperty("db.url")?
- a) It retrieves the value of the property
- b) It deletes the property
7. Why use classpath loading instead of absolute file paths?
- a) Works everywhere, including inside JAR files
- b) Only works on Windows