Mastering Spring Boot 2.0
上QQ阅读APP看书,第一时间看更新

Understanding auto-configuration

Spring Boot auto-configuration provides automatic configuration to your Spring application based on the modules and associated library dependencies of those modules that you have added. For instance, if you have added the embedded in-memory database H2 in your classpath, you are not required to manually configure any bean related to the database such as DataSource, JdbcTemplate, and so on. Spring Boot provides your H2 database with auto-configuration after adding dependency on the H2 database in your application's classpath.

Spring Boot provides the magic of autoconfiguration by extensive use of pre-written @Configuration classes for each module of Spring Framework. But these auto-configurations are activated based on:

  • The contents of the classpath of your Spring application
  • Properties you have set in the application
  • Beans already defined in your application

The @Profile annotation of Spring Framework is an example of conditional configuration. Spring Boot takes this idea to the next level and provides a layer of auto-configuration on top of the traditional Spring Framework. That is why Spring Boot is not a separate framework by heart; it is Spring Framework.

@Profile is a special case of @Conditional.