How to load custom properties files in Spring Boot

This article shows how to use @PropertySource annotation to load custom properties files in Spring Boot. Table of contents: 1. Load default properties file 2. Load custom properties files 3. Load multiple custom properties files 4. Download Source Code 5. References P.S. Tested with Spring Boot 3.1.2 1. Load default properties file Spring default loads …

Read more

Spring @TestPropertySource example

In Spring integration tests, we can use @TestPropertySource to override the application properties which default loaded from application.properties or application.yml. Table of contents 1. Service Class 2. Test with the @TestPropertySource 3. @TestPropertySource Inline Properties 4. Download Source Code 5. References P.S Tested with Spring Boot 3.1.2 1. Service Class 1.1 We have a service …

Read more

logging.properties example

The Java logging APIs (java.util.logging) default loads logging.properties in the $JAVA_HOME/jre/lib/ (Java 8 and before); for Java 9 and above, the logging.properties file moved to $JAVA_HOME/conf. Note Java Logging APIs Tutorial logging.properties Here is the logging.properties file from Java 11. C:\opt\jdk-11.0.1\conf ############################################################ # Default Logging Configuration File # # You can use a different file …

Read more

Spring Boot YAML example

In this article, we will show you how to use YAML instead of properties file in Spring Boot. Tested with : Spring Boot 2.1.2.RELEASE Maven 3 Snakeyaml:jar:1.23 In short, create a application.yml in the src/resources folder, Spring Boot will load and parse .yml file automatically and bind the values into the classes which annotated with …

Read more

Spring @Value – Import a list from properties file

In this tutorial, we will show you how to import a “List” from a properties file, via Spring EL @Value Tested with : Spring 4.0.6 JDK 1.7 Spring @Value and List In Spring @Value, you can use the split() method to inject the ‘List” in one line. config.properties server.name=hydra,zeus server.id=100,102,103 AppConfigTest.java package com.mkyong.analyzer.test; import java.util.List; …

Read more

Spring @PropertySource example

Spring default loads application.properties into the application’s environment, and we can use @PropertySource to load custom .properties files. file.properties file.path=/server1/file/path Application.java @Configuration @PropertySource("classpath:file.properties") public class Application { @Value("${file.path}") private String path; //… } Table of contents: 1. Project Structure 2. Properties Files 3. @PropertySource and @Value 4. Loads property files from multiple sources 5. Placeholder …

Read more

Java : getResourceAsStream in static method

To call getResourceAsStream in a static method, we use ClassName.class instead of getClass() 1. In non static method getClass().getClassLoader().getResourceAsStream("config.properties")) 2. In static method ClassName.class.class.getClassLoader().getResourceAsStream("config.properties")) 1. Non Static Method A .properties file in project classpath. src/main/resources/config.properties #config file json.filepath = /Users/mkyong/Documents/workspace/SnakeCrawler/data/ FileHelper.java package com.mkyong; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class FileHelper { public static …

Read more