官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 晋州市| 青川县| 麦盖提县| 大同市| 鄂托克旗| 河北省| 静安区| 扬中市| 泰来县| 泊头市| 青铜峡市| 察雅县| 石林| 通辽市| 新建县| 昌乐县| 拜泉县| 岑巩县| 雷山县| 禹州市| 肥乡县| 墨脱县| 绥宁县| 西乌珠穆沁旗| 安徽省| 灵武市| 嘉善县| 达尔| 泰来县| 枣阳市| 大兴区| 茌平县| 迁安市| 新兴县| 屯留县| 嘉义市| 嫩江县| 普宁市| 民勤县| 江都市| 措美县|