- Mastering Spring Boot 2.0
- Dinesh Rajput
- 368字
- 2021-06-25 21:29:11
Enabling Spring Boot auto-configuration
Spring Boot provides the @EnableAutoConfiguration annotation that is responsible for enabling the auto-configuration feature. This annotation is used in the main application file of the Spring Boot application. The @EnableAutoConfiguration annotation on a Spring Java configuration class causes Spring Boot to automatically create beans it thinks you need, usually based on classpath contents, that it can easily override.
Let's see the following code that represents the main application launcher class in the Spring Boot application:
@Configuration @EnableAutoConfiguration public class MyAppConfig { public static void main(String[] args) { SpringApplication.run(MyAppConfig.class, args); } }
But, Spring Boot also provides a shortcut for this configuration file by using another annotation, @SpringBootApplication.
It is very common to use @EnableAutoConfiguration, @Configuration, and @ComponentScan together. Let's see the following updated code:
@SpringBootApplication public class MyAppConfig { public static void main(String[] args) { SpringApplication.run(MyAppConfig.class, args); } }
In this code, @ComponentScan, with no arguments, scans the current package and its sub-packages.
@SpringBootApplication has been available since Spring Boot 1.2.
Let's see the following diagram to explain it better than the code:

In this diagram, we can say that the @SpringBootApplication annotation has composed functionality from three annotations—@EnableAutoConfiguration, @ComponentScan, and @Configuration.
Spring Boot Starter reduces a build's dependencies and Spring Boot auto-configuration reduces the Spring configuration.
If you want to exclude auto-configuration for some of the modules, then you use the exclude property of @SpringBootAnnotation. Let's look at the following code:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class MyAppConfig { ... }
As you can see in the code, this Spring Boot application will consider DataSourceAutoConfiguration.class and HibernateJpaAutoConfiguration.class for the auto-configuration.
Let's see the following diagram that explains all about the Spring Boot auto-configuration feature:

As you can see in the diagram, you just include the required modules in your Spring application. At runtime, Spring Boot checks libraries at the classpath of your application. If the required libraries are available on the classpath of your application, then Spring Boot configures the required beans and other configuration for your application. You don't need to worry about the configuration of the modules in the Spring Boot application.
Let's discuss another key component, Spring Boot CLI, in the next section.
- Word/Excel/PPT 2007辦公技巧
- 五筆打字精品教程
- Excel函數(shù)與公式標(biāo)準(zhǔn)教程(實(shí)戰(zhàn)微課版)
- Office 2016辦公應(yīng)用從入門到精通
- Excel 2016表格、公式、函數(shù)與圖表應(yīng)用傻瓜書
- Office 2016從新手到高手
- 非常Easy:Excel行政與文秘高效辦公
- Photoshop+Illustrator平面設(shè)計(jì)案例實(shí)戰(zhàn)從入門到精通:視頻自學(xué)全彩版
- Word/Excel/PowerPoint 2013三合一辦公應(yīng)用
- 和秋葉一起學(xué)Excel(第2版)
- PPT制作應(yīng)用大全
- Excel高手捷徑 一招鮮 吃遍天
- 中文版Office 2016三合一辦公基礎(chǔ)教程
- PPT進(jìn)化:如何設(shè)計(jì)一份驚艷的PPT
- 數(shù)據(jù)分析:企業(yè)的賢內(nèi)助