liveBook · Manning

Application properties

As explained in Spring in Action, Sixth Edition

application.properties is a conventional Spring Boot configuration file that expresses application settings as properties.

`application.properties` is a place to set values for a Spring Boot application. It sits in `src/main/resources` and may be empty when a project is created. Add a property when you want to change a setting without writing bean-configuration code. section 1.2.2 section 6.1

Change the server port

Put `server.port=9090` in `src/main/resources/application.properties` to make the application listen on port 9090 instead of the default 8080. The equivalent YAML file, `application.yml`, expresses the same setting as `server:` followed by an indented `port: 9090`. section 6.1.1

The file is one source of configuration values, not the only one. Spring’s environment also gathers JVM system properties, operating-system environment variables, and command-line arguments, then makes the values available to beans. For example, `--server.port=9090` sets the port at launch, while `SERVER_PORT=9090` sets it through an environment variable. section 6.1.1

Properties can tune automatically configured components without declaring replacement beans:

  • Set a database URL and credentials with `spring.datasource` properties; Spring Boot uses them when configuring a `DataSource`. section 6.1.2
  • Set logger levels with `logging.level` properties or a log-file path with `logging.file.name`, without creating a `logback.xml` file for those changes. section 6.1.4
  • Use `${spring.application.name}` inside another property value when that value should incorporate the application name. section 6.1.5

Your own beans can accept values too. Annotate a Spring bean with `@ConfigurationProperties(prefix="taco.orders")`, give it a `pageSize` property and setter, and set `taco.orders.pageSize` in configuration. An order-list controller can then use that value instead of hardcoding a page size of 20. section 6.2

For settings that differ by environment, keep common properties in `application.properties` and put profile-specific ones in a file such as `application-prod.properties`. A property for the active profile can replace a common value. YAML files support the same naming approach. section 6.3.1

If an IDE flags a custom property as unknown, the property can still work. Optional metadata in `src/main/resources/META-INF/additional-spring-configuration-metadata.json` supplies descriptions and IDE help. section 6.2.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Works through a port setting, component settings, custom property binding, and profile-specific files; leaves detailed Logback configuration aside.
Examples
Uses `server.port=9090`, database connection properties, logging settings, and an order-list page-size property.
What it assumes
Starts with an initially empty project configuration file and introduces Spring’s property sources before showing how beans consume their values.
What you can do after
Operate/configure a Spring Boot application by setting component values and separating common from profile-specific properties.
How it connects
Connects application properties to Spring’s environment abstraction, autoconfiguration, @ConfigurationProperties, and Spring profiles.

Also explained in

Compare how 2 books explain application properties →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.