- Mastering Spring Boot 2.0
- Dinesh Rajput
- 563字
- 2021-06-25 21:29:11
Spring Boot auto-configuration
Spring Boot can automatically provide configuration for application functionality, which is common to many Spring applications. Auto-configuration works by analyzing the classpath as follows:
- If you forget a dependency, Spring Boot can't configure it
- A dependency management tool is recommended
- Spring Boot Parent and Starters make it much easier
- Spring Boot works with Maven, Gradle, and Ant/Ivy
Spring Boot offers auto-configuration of those modules in your Spring application based on the JAR dependencies that you have added. Suppose you added the JPA starter dependency (spring-boot-starter-data-jpa) in your Spring application classpath; Spring Boot attempts to automatically configure JPA to your Spring application. Now, you have not manually configured any database connection beans related to JPA. Similarly, if you want to add an in-memory database such as HSQLDB, just add it (org.hsqldb) in the classpath of your Spring application, and it will auto-configure an in-memory database.
Spring Boot provides the auto-configuration feature in the following ways:
- First, Spring Boot looks for frameworks available on the classpath
- After that, it checks existing configuration for the application
Based on these points, Spring Boot provides the basic configuration needed to configure the application with these frameworks. This is called auto-configuration.
In another book, Spring 5 Design Patterns, I have written an application related to the backend that accesses a relational database by using JDBC. As we know that the Spring Framework provides JdbcTemplate, we have to register this JdbcTemplate as a bean in the application context of our application as follows:
@Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }
This configuration creates an instance of JdbcTemplate and injects it with another bean dependency, DataSource. So, also we have to register this DataSource bean to be met. Let's see in the following configuration how the HSQL database is configured with a DataSource bean:
@Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.HSQL) .addScripts('schema.sql', 'data.sql') .build(); }
This configuration creates an instance of DataSource specifying the SQL scripts schema.sql and data.sql with the HSQL embedded database.
You can see that the two bean methods are not too complex to define, but are also not part of the application logic. This represents just a fraction of application configuration. If you add the Spring MVC module to the same application, then you have to register another corresponding bean method. These methods will be the same for each Spring application where you want to use the same modules. We can say that this is boilerplate configuration code in each Spring application.
In short, the configuration, whatever we have defined, is a common configuration for each application. Ideally, we should not have to write it for each application.
Spring Boot addresses this problem of common configuration. It can automatically configure these common configuration bean methods. Spring Boot provides this auto-configuration based on the available library in your application's classpath. So, if we have to add the HSQL database library in your application's classpath, then it will automatically configure an embedded HSQL database.
If a Spring JDBC-related library is in the classpath of your Spring application, then it will also configure a JdbcTemplate bean for your application. There is no need to configure these beans manually in your Spring application. These beans will be automatically configured for you; just use them for business logic. Spring Boot reduces such boilerplate code configuration at the developer's end.
- Excel完全自學教程
- WPS辦公應用四合一:文檔處理+數據分析+文稿演示+移動辦公
- Access 2010數據庫實用教程
- Office 2016從新手到高手
- Excel 2010高效辦公:市場與銷售管理
- Excel 必修課:Excel表格制作與數據分析
- OpenCV Essentials
- WPS Office辦公應用從入門到精通
- Excel辦公高手應用技巧
- Final Cut Pro X基礎培訓教程
- Photoshop+Illustrator平面設計案例實戰從入門到精通:視頻自學全彩版
- 精通Excel數據統計與分析
- 電腦上網技巧現用現查
- Mapping and Visualization with SuperCollider
- Word 2007辦公應用融會貫通