Understanding Application Properties

As we know Spring Boot configures almost all the configurations automatically, it also enables us to customize the configurations and properties according to our needs and environment. There are various methods through which we can use to do the same.

Either we can write all properties in a textual file, do programmatically in our Java classes, or can set it while starting the application through CLI by passing command-line arguments.

By default, Spring Initializr creates an application.properties file inside the classpath of the project. But we can also define it in the YAML file.

Know the differences between the .properties file and .yml file

The properties that we are talking about are database credentials and URL, server port, logging file path, catch control variables, can write any custom string constants, etc.

 Method 1: Using application.properties

Sample of application.properties file



#custom constant

 Method 2: Using application.yml file

YAML is a superset of JSON and, as such, is a convenient format for specifying hierarchical configuration data. The SpringApplication class automatically supports YAML as an alternative to properties whenever you have the SnakeYAML library on your classpath.

Sample of application.yml file


url: jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=false
username: root
password: demo
driver-class-name: com.mysql.cj.jdbc.Driver
ddl-auto: update
show-sql: false

port: 8089

 Method 3: Using the Java class

Setting up the application properties from the Java classes is up to you that how you like to do, you may do it through making a component and assign all the properties in beans. Components are executed at first priority while starting the web applications.

Sample of Class file

@Configuration(proxyBeanMethods = false)
public class ProductionConfiguration {
             private String host; 
             private int port; 
             private String from; 
             private List<String> defaultRecipients; 
             private Map<String, String> additionalHeaders; 
             private Credentials credentials; 
             // standard getters and setters


 Method 4: Using command-line arguments

This method applies after building the application jar. While running the jar from CLI write all the properties to the JSON format as shown below example

$ java -jar myapp.jar --spring.application.json='{"server.port":8084,"spring.jpa.show-sql":true}'


 How many properties do we need to write in an application? 

There are lots of properties that you can write inside your application.properties file, inside your application.yml file, or as command-line switches. It does not have any number, it depends upon your customization, and dependencies included in your application. Almost all the properties have a default value preset by the auto-configure feature of the spring boot. The number of dependencies you add the number of properties increases. Also, you can define your own properties.

In Spring Boot official documentation the list common application properties are described.