📜  Spring Boot – 自动配置

📅  最后修改于: 2022-05-13 01:54:45.488000             🧑  作者: Mango

Spring Boot – 自动配置

Spring Boot 之所以吸引开发人员,是因为它具有以下三个主要特性:

  1. 自动配置——例如检查依赖关系、类路径中某些类的存在、bean 的存在或某些属性的激活。
  2. 一种自以为是的配置方法。
  3. 创建独立应用程序的能力。

Spring Boot 中的自动配置

  • @Conditional 注解充当 Spring Boot 自动配置注解扩展的基础。
  • 它会自动使用@Component、@Configuration、@Bean 和元注释注册 bean,以构建自定义构造型注释等。
  • 注解@EnableAutoConfiguration 用于启用自动配置功能。
  • @EnableAutoConfiguration 注解通过扫描类路径组件和注册 bean 来启用 Spring ApplicationContext 的自动配置。
  • 此注解与@ComponentScan 和@SpringBootConfiguration 注解一起包装在@SpringBootApplication 注解中。
  • 运行 main() 方法时,此注解会启动自动配置。

实现:应用程序的引导

Java
// Java Program to Illustrate Bootstrapping of Application
  
package gfg;
  
// Importing required classes
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
  
// Annotation
@SpringBootApplication
  
// Class
public class GfgApplication {
  
    // Main driver method
    public static void main(String[] args)
    {
        SpringApplication.run(GfgApplication.class, args);
    }
}


XML


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    sia
    taco-cloud
    0.0.1-SNAPSHOT
    taco-cloud
    Demo project for Spring Boot
    
        11
        14.7.5
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
  
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-jersey
        
        
            org.springframework.boot
            spring-boot-starter-web-services
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            com.vaadin
            vaadin-spring-boot-starter
        
        
            io.projectreactor
            reactor-test
            test
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            com.h2database
            h2
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.security
            spring-security-test
            test
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-hateoas
        
        
            org.springframework.boot
            spring-boot-starter-data-rest
        
    
  
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    
  
    
        
            
                com.vaadin
                vaadin-bom
                ${vaadin.version}
                pom
                import
            
        
    
    
        
            production
            
                
                    
                        com.vaadin
                        vaadin-maven-plugin
                        ${vaadin.version}
                        
                            
                                frontend
                                compile
                                
                                    prepare-frontend
                                    build-frontend
                                
                                
                                    true
                                
                            
                        
                    
                
            
        
    


Java
// Java Program Illustrating Configuration of
// DataSourceConfiguration of DataSource
  
package gfg;
  
// Importing required classes
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
  
// Annotation
@Configuration
  
// Class
public class ConfigDataSource {
  
    // Annotation
    @Bean public static DataSource source()
    {
  
        DataSourceBuilder dSB
            = DataSourceBuilder.create();
        dSB.driverClassName("com.mysql.jdbc.Driver");
  
        // MySQL specific url with database name
        dSB.url("jdbc:mysql://localhost:3306/userdetails");
  
        // MySQL username credential
        dSB.username("user");
  
        // MySQL password credential
        dSB.password("password");
  
        // builds and returns a new
        // configured datasource object
        return dSB.build();
    }
}


Spring Boot 中自动配置的工作

A:依赖关系

  • 自动配置是 Spring Boot 开发的主要重点。
  • 我们的 Spring 应用程序需要一组相应的依赖项才能工作。
  • Spring Boot 会自动配置预先设置的所需依赖项,而无需手动配置它们。
  • 当我们想要创建一个独立的应用程序时,这非常有帮助并且可以看到。
  • 当我们构建应用程序时,Spring Boot 会处理我们的依赖项,并根据我们构建的项目在类路径上配置底层 Spring Framework 和所需的 jar 依赖项(第三方库)。
  • 它可以帮助我们避免不同库的不匹配或不兼容版本等错误。
  • 如果要覆盖这些默认值,可以在初始化后覆盖它们。

工具:Maven

示例 1:pom.xml

XML



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    sia
    taco-cloud
    0.0.1-SNAPSHOT
    taco-cloud
    Demo project for Spring Boot
    
        11
        14.7.5
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
  
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-jersey
        
        
            org.springframework.boot
            spring-boot-starter-web-services
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            com.vaadin
            vaadin-spring-boot-starter
        
        
            io.projectreactor
            reactor-test
            test
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            com.h2database
            h2
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.security
            spring-security-test
            test
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-hateoas
        
        
            org.springframework.boot
            spring-boot-starter-data-rest
        
    
  
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    
  
    
        
            
                com.vaadin
                vaadin-bom
                ${vaadin.version}
                pom
                import
            
        
    
    
        
            production
            
                
                    
                        com.vaadin
                        vaadin-maven-plugin
                        ${vaadin.version}
                        
                            
                                frontend
                                compile
                                
                                    prepare-frontend
                                    build-frontend
                                
                                
                                    true
                                
                            
                        
                    
                
            
        
    

了解依赖项的自动配置

  • 当您构建 Spring Boot 项目时,“Starter Parent”依赖项会自动添加到“pom.xml”文件中。
  • 它通知应用程序的基本“合理”默认值已自动配置,因此您可以利用它。

org.springframework.boot
spring-boot-starter-parent
...
  • 要添加依赖项(技术堆栈库),您无需提及它的版本,因为 Spring Boot 会自动为您配置它。
  • 此外,当您更新/更改 Spring Boot 版本时,所有添加的依赖项的版本也将得到更新/更改。

org.springframework.boot
spring-boot-starter-thymeleaf
  • 正是 Spring Boot 的自动配置使管理依赖关系对我们来说非常容易。
  • 借助在“application.properties”文件中启用“调试日志”,我们可以了解更多关于自动配置的信息。
logging.level.org.springframework: DEBUG  

工具 B:Gradle

示例 2:build.gradle

buildscript {
   repositories {
       jcenter()
   }

   dependencies {
       classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.8.RELEASE")
   }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

repositories {
   jcenter()
}

dependencies {
   compile("org.springframework.boot:spring-boot-starter-web")
   testCompile("org.springframework.boot:spring-boot-starter-test")
}

B:弹簧应用

插图:  

  1. @Bean 是一个方法级别的注解。
  2. @Bean 注解指定一个方法生成一个注册为 Bean(数据)的返回值,BeanFactory 由 Spring Container 管理。
  3. 这个特定的Java程序使用 @Configuration 注解指定该类包含一个或多个 @Bean 注解,这些注解有助于在 Spring Container(Spring Application Context)中自动注册(初始化)。
  4. @Configuration 是一个类级别的注解。

例子

Java

// Java Program Illustrating Configuration of
// DataSourceConfiguration of DataSource
  
package gfg;
  
// Importing required classes
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
  
// Annotation
@Configuration
  
// Class
public class ConfigDataSource {
  
    // Annotation
    @Bean public static DataSource source()
    {
  
        DataSourceBuilder dSB
            = DataSourceBuilder.create();
        dSB.driverClassName("com.mysql.jdbc.Driver");
  
        // MySQL specific url with database name
        dSB.url("jdbc:mysql://localhost:3306/userdetails");
  
        // MySQL username credential
        dSB.username("user");
  
        // MySQL password credential
        dSB.password("password");
  
        // builds and returns a new
        // configured datasource object
        return dSB.build();
    }
}