Blog logo on a black background with the words "Discover What's Next in Tech!"

Convention over Configuration in Spring Boot

Complex configurations for a simple test application are a thing of the past: With Convention over Configuration in Spring Boot the complexity of configurations can be greatly reduced: As long as you adhere to the conventions specified by the framework, many manual configurations are no longer necessary. For example, the appropriate Spring Beans are created if certain classes are contained in the path. If, on the other hand, the appropriate beans already exist due to a manual configuration, they are not created. [1]

One Spring Boot application can therefore be quickly made executable using the standard configurations defined by the framework, but the developer can also easily overwrite these.

Externalized Configuration

Spring Boot also makes it possible to move the configuration of the application to the outside and thus use the same application code in different environments. For example, central configurations such as the database connection, SSL encryption, ports or log files can be defined via the application.properties file. When the application is started, Spring Boot uses the configuration file within the generated .jar file. However, if there is an external application.properties file that is stored outside the .jar file, the configuration is retrieved from this file by default. This makes it easy to overwrite central configurations and the application code can be used in different environments without having to change the .jar file. It is also possible to create profile-specific application.properties files, which are then used for the respective profile.

Database initialization

The framework can also be used to initialize an SQL database. To do this, a schema.sql file containing the necessary table definitions must be created in the resource folder. A data.sql file is also required in the same folder to populate the database. During initialization Spring Boot for these files and executes them with the database initialization.

Even if the database tables cannot be accessed via Spring Boot the data.sql file can be helpful to provide test data for the test database and ensure that it is always identical. To do this, the data.sql file is stored in the resource folder under Test.

With Spring Data JPA repository implementations can be generated automatically from an interface at runtime. The type of entity is then defined and the repository already contains the basic functionalities without the need to write additional code: Finding, saving and deleting the respective entity. [2]

Conclusion

Convention over Configuration is one of the central concepts of Spring Boot. This makes it possible to Spring Boot applications with very little code as long as the standard configurations are used. In the best case scenario, the developer only has to learn the conventions behind the framework and does not have to deal with countless configurations.


Sources:

[1] https://spring.io/guides/gs/spring-boot/

[2] https://spring.io/guides/gs/accessing-data-jpa/

Melanie Müller

About ME

All contributions from Melanie Müller

Learn more

Further information on our website and in our newsletter

Arrow up