Spring Boot @ServiceConnection Example

Spring Boot 3.1 introduced @ServiceConnection to enable the Spring Boot’s auto-configuration to use the service connection details to connect to a remote service (Testcontainers). Tables of contents: 1. Testing with @DynamicPropertySource 2. Spring Boot 3.1 and @ServiceConnection 3. Download Source Code 4. References 1. Testing with @DynamicPropertySource The following example uses @DynamicPropertySource to register the …

Read more

log4j2.properties example

A simple log4j2.properties example, just for self-reference P.S Tested with Log4j 2.11.2 src/resources/log4j2.properties status = warn appender.console.type = Console appender.console.name = LogToConsole appender.console.layout.type = PatternLayout appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n #appender.file.type = File #appender.file.name = LogToFile #appender.file.fileName=logs/app.log #appender.file.layout.type=PatternLayout #appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n # Rotate log file appender.rolling.type = …

Read more

Spring Boot – Profile based properties and yaml example

In Spring Boot, it picks .properties or .yaml files in the following sequences : application-{profile}.properties and YAML variants application.properties and YAML variants Note For detail, sequence and order, please refer to this official Externalized Configuration documentation. Tested : Spring Boot 2.1.2.RELEASE Maven 3 1. Project Structure A standard Maven project structure. 2. @ConfigurationProperties Read the …

Read more

Spring Boot @ConfigurationProperties example

Spring Boot @ConfigurationProperties lets developers map or bind the entire external configuration values in .properties or .yml files to Java objects. Table of contents: 1. Access a single value using @Value 2. @ConfigurationProperties 3. Add JSR303 Validation 3.1 Add JSR 303 dependencies 3.2 @ConfigurationProperties and @Validated 4. Testing @ConfigurationProperties 5. Download Source Code 6. References …

Read more

Spring – ${} is not working in @Value

A simple Spring @PropertySource example to read a properties file. db.properties db.driver=oracle.jdbc.driver.OracleDriver AppConfig.java @Configuration @PropertySource("classpath:db.properties") public class AppConfig { @Value("${db.driver}") private String driver; But the property placeholder ${} is unable to resolve in @Value, if print out the driver variable, it will display string ${db.driver} directly, instead of “oracle.jdbc.driver.OracleDriver”. Solution To resolve ${} in Spring …

Read more

How to create user defined properties in Maven

Custom properties or variables are useful to keep your Maven pom.xml file more easy to read and maintain. File : pom.xml <project> … <properties> <my.plugin.version>1.5</my.plugin.version> </properties> … </project> In above pom.xml, you can refer “my.plugin.version” via code ${my.plugin.version}. Example 1 A classic use case is used to define a jar or plugin version. <properties> <spring.version>3.1.2.RELEASE</spring.version> …

Read more

How to use reflection to copy properties from Pojo to other Java Beans

Sometimes we need copy properties from a Java class to other, we can do it this manually or with our own reflection implementation, but in this case use us reflection for automate it with a utility from apache Requirements commons-beanutils , you can download from here http://commons.apache.org/beanutils/ commons-loging , you can download from here http://commons.apache.org/logging/ …

Read more

Java Properties file examples

Normally, Java properties file is used to store project configuration data or settings. In this tutorial, we will show you how to read and write to/from a .properties file. Properties prop = new Properties(); // set key and value prop.setProperty("db.url", "localhost"); prop.setProperty("db.user", "mkyong"); prop.setProperty("db.password", "password"); // save a properties file prop.store(outputStream, ""); // load a …

Read more

Java – How to display all System properties

In Java, you can use System.getProperties() to get all the system properties. Properties properties = System.getProperties(); properties.forEach((k, v) -> System.out.println(k + ":" + v)); // Java 8 1. Example DisplayApp.java package com.mkyong.display; import java.util.Properties; public class DisplayApp { public static void main(String[] args) { Properties properties = System.getProperties(); // Java 8 properties.forEach((k, v) -> System.out.println(k …

Read more