AI Hints
Home›Spring Boot Tutorial›application.properties, @Value and @ConfigurationProperties
Spring Boot · Lesson 37 of 120

application.properties

Settings outside the code.

⏱ 2 min read▶ Video at 48:59🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Some values change between computers, or over time. A port, a shop name, a discount. These belong in a settings file, not inside the Java code.

  • Values that change
  • Ports, names, discounts
  • A settings file

Why settings files

You can change a setting, without changing or rebuilding the code. The same code can run with different settings, on your laptop, and on a server. And all the settings live in one place, easy to find.

keyvalue
Changewithout touching code
Same codedifferent settings per place
One placeeasy to find
Filesrc/main/resources/application.properties

Built-in settings

Spring Boot understands hundreds of settings. Server port sets the port. Server servlet context path adds a prefix, to every URL. Spring application name names the app. And logging level sets how much is logged.

KeyExample
server.port8081
server.servlet.context-path/shop
spring.application.namebookshop
logging.level.<package>DEBUG

Changing a built-in setting

Our properties file sets the app name. It sets a context path, of slash shop. And it adds two settings of our own, shop name, and shop discount. We'll read those in the next lesson. Run it. Now our hello endpoint lives at slash shop slash hello. And plain slash hello is not found anymore.

application.propertiesini
spring.application.name=bookshop
server.servlet.context-path=/shop
shop.name=Corner Books
shop.discount=10
Output
$ curl localhost:8080/shop/hello
Hello from the shop
$ curl -i localhost:8080/hello | head -1
HTTP/1.1 404
Also uses HelloController.java
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from the shop";
    }
}
Also uses _curl.sh
curl localhost:8080/shop/hello
curl -i localhost:8080/hello | head -1

Summary

Application dot properties holds key equals value settings. Spring Boot has many built in keys, and you can add your own. Next, reading them with at Value.

Key takeaways
  • key=value
  • Built-in keys
  • Your own keys
Next up: @Value — read one setting