Advertisement

Google Ad Slot: content-top

Spring Boot Application Properties

~1 min read · Spring Boot
On this page

Spring Boot provides a flexible way to configure applications using application.properties or application.yml files. These configuration files help manage database settings, logging, server properties, and more.


Where to Place application.properties?

  • Inside the src/main/resources/ directory.
  • Spring Boot automatically loads this file at runtime



Common Configurations in application.properties

1. Server Configuration

🔹 server.port → Changes the default port (8080 → 8081).

🔹 server.servlet.context-path → Sets the base URL (localhost:8081/myapp).

server.port=8081 server.servlet.context-path=/myapp

2. Database Configuration (MySQL Example)

🔹 spring.datasource.url → Database connection URL.

🔹 spring.jpa.database-platform → Specifies the Hibernate dialect.

spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect

3. Hibernate & JPA Configuration

🔹 spring.jpa.show-sql → Prints SQL queries in the console.

🔹 spring.jpa.hibernate.ddl-auto → Schema management (update, create, none).

🔹 spring.jpa.properties.hibernate.format_sql → Formats SQL queries.

spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.format_sql=true

4. Logging Configuration

🔹 logging.level.root → Set logging level (TRACE, DEBUG, INFO, WARN, ERROR).

🔹 logging.file.name → Save logs to logs/app.log.

logging.level.root=INFO logging.level.org.springframework=DEBUG logging.file.name=logs/app.log

Using application.yml (YAML Format)

Equivalent YAML Configuration for the Above Properties:

server: port: 8081 servlet: context-path: /myapp spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: driver-class-name: com.mysql.cj.jdbc.Driver jpa: show-sql: true hibernate: ddl-auto: update properties: hibernate: format_sql: true logging: level: root: INFO org.springframework: DEBUG file: name: logs/app.log