Spring Boot by default loads properties from application.properties. In this tutorial we will see what are additional ways to pass application properties. We are going to cover some of the important options as stated here.
Examples
pom.xml<project .....> <modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId> <artifactId>boot-different-ways-to-pass-app-properties</artifactId> <version>1.0-SNAPSHOT</version>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent>
<properties> <java.version>1.8</java.version> <theMainClass>com.logicbig.example.ExampleMain</theMainClass> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${theMainClass}</mainClass> </configuration> </plugin> </plugins> </build> </project>
@Component
class MyBean {
@Value("${app.title}")
private String appTitle;
@PostConstruct
public void startApplication() {
System.out.printf("-- running application: %s --%n", appTitle);
}
}
Using application.properties
src/main/resources/application.propertiesapp.title=My App with application.properties
@SpringBootApplication
public class ExampleMain {
public static void main(String[] args) {
SpringApplication.run(ExampleMain.class, args);
}
} . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 16:20:50.889 INFO 8780 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain on DESKTOP-VCSEKL5 with PID 8780 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\classes started by Joe in d:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 16:20:50.894 INFO 8780 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: My App with application.properties -- 2020-03-14 16:20:51.826 INFO 8780 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 1.459 seconds (JVM running for 1.948)
Via System properties
D:\boot-different-ways-to-pass-app-properties>mvn -q spring-boot:run -DtheMainClass="com.logicbig.example.ExampleMain" -Dspring-boot.run.jvmArguments="-Dapp.title='My app via JVM option'"
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 17:41:41.747 INFO 11644 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain on DESKTOP-VCSEKL5 with PID 11644 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\classes started by Joe in D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 17:41:41.750 INFO 11644 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: My app via JVM option -- 2020-03-14 17:41:42.269 INFO 11644 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 0.952 seconds (JVM running for 1.386)
Executing Jar
D:\boot-different-ways-to-pass-app-properties>mvn -q clean package spring-boot:repackage
D:\boot-different-ways-to-pass-app-properties>java -jar -Dapp.title="My App with System properties and exec jar" target/boot-different-ways-to-pass-app-properties-1.0-SNAPSHOT.jar
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 17:49:47.466 INFO 18208 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain v1.0-SNAPSHOT on DESKTOP-VCSEKL5 with PID 18208 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\boot-different-ways-to-pass-app-properties-1.0-SNAPSHOT.jar started by Joe in D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 17:49:47.469 INFO 18208 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: My App with System properties and exec jar -- 2020-03-14 17:49:48.303 INFO 18208 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 1.496 seconds (JVM running for 2.114)
Programmatically:
@SpringBootApplication
public class ExampleMain2 {
public static void main(String[] args) {
System.setProperty("app.title", "My Spring Application with system properties --");
SpringApplication.run(ExampleMain2.class, args);
}
} . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 17:51:39.063 INFO 5400 --- [ main] com.logicbig.example.ExampleMain2 : Starting ExampleMain2 on DESKTOP-VCSEKL5 with PID 5400 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\classes started by Joe in d:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 17:51:39.069 INFO 5400 --- [ main] com.logicbig.example.ExampleMain2 : No active profile set, falling back to default profiles: default -- running application: My Spring Application with system properties -- -- 2020-03-14 17:51:39.876 INFO 5400 --- [ main] com.logicbig.example.ExampleMain2 : Started ExampleMain2 in 1.314 seconds (JVM running for 1.789)
Using SpringApplication#setDefaultProperties
@SpringBootApplication
public class ExampleMain3 {
public static void main(String[] args) {
System.setProperty("spring.config.location", "");// commenting this line will use application.properties
SpringApplication sa = new SpringApplication(ExampleMain3.class);
Properties properties = new Properties();
properties.setProperty("app.title", "My App with SpringApplication's default properties");
sa.setDefaultProperties(properties);
sa.run(args);
}
} . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 17:53:08.581 INFO 13604 --- [ main] com.logicbig.example.ExampleMain3 : Starting ExampleMain3 on DESKTOP-VCSEKL5 with PID 13604 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\classes started by Joe in d:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 17:53:08.586 INFO 13604 --- [ main] com.logicbig.example.ExampleMain3 : No active profile set, falling back to default profiles: default -- running application: My App with SpringApplication's default properties -- 2020-03-14 17:53:09.612 INFO 13604 --- [ main] com.logicbig.example.ExampleMain3 : Started ExampleMain3 in 1.495 seconds (JVM running for 1.978)
Note that we provided empty spring.config.location . This property specifies the application.properties location. Since application.properties overrides SpringApplication's default properties and we already have application.properties from our first example, we provided empty value for spring.config.location .
Using command line application arguments
D:\boot-different-ways-to-pass-app-properties>mvn -q spring-boot:run -Dspring-boot.run.arguments="--app.title='My App with application arguments'"
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 18:06:54.527 INFO 292 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain on DESKTOP-VCSEKL5 with PID 292 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\classes started by Joe in D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 18:06:54.530 INFO 292 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: My App with application arguments -- 2020-03-14 18:06:54.967 INFO 292 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 0.76 seconds (JVM running for 1.155)
Executing Jar
D:\boot-different-ways-to-pass-app-properties>java -jar target/boot-different-ways-to-pass-app-properties-1.0-Snapshot.jar --app.title="My app with application arguments and exec jar"
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 18:09:25.234 INFO 2660 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain v1.0-SNAPSHOT on DESKTOP-VCSEKL5 with PID 2660 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\boot-different-ways-to-pass-app-properties-1.0-SNAPSHOT.jar started by Joe in D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 18:09:25.237 INFO 2660 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: My app with application arguments and exec jar -- 2020-03-14 18:09:25.993 INFO 2660 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 1.3 seconds (JVM running for 1.822)
Using OS environment variables
D:\boot-different-ways-to-pass-app-properties>set app.title="My App with O.S env variable"
D:\boot-different-ways-to-pass-app-properties>mvn -q spring-boot:run . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 18:16:29.189 INFO 7716 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain on DESKTOP-VCSEKL5 with PID 7716 (D:\boot-different-ways-to-pass-app-properties\target\classes started by Joe in D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 18:16:29.192 INFO 7716 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: "My App with O.S env variable" -- 2020-03-14 18:16:29.639 INFO 7716 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 0.763 seconds (JVM running for 1.054)
Executing jar after setting same env variable:
D:\boot-different-ways-to-pass-app-properties>java -jar target/boot-different-ways-to-pass-app-properties-1.0-Snapshot.jar
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.5.RELEASE)
2020-03-14 18:17:34.834 INFO 12948 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain v1.0-SNAPSHOT on DESKTOP-VCSEKL5 with PID 12948 (D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties\target\boot-different-ways-to-pass-app-properties-1.0-SNAPSHOT.jar started by Joe in D:\example-projects\spring-boot\boot-different-ways-to-pass-app-properties) 2020-03-14 18:17:34.840 INFO 12948 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default -- running application: "My App with O.S env variable" -- 2020-03-14 18:17:35.468 INFO 12948 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 1.164 seconds (JVM running for 1.638)
Example ProjectDependencies and Technologies Used: - Spring Boot 2.2.5.RELEASE
- spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
- JDK 1.8
- Maven 3.5.4
|