Spring开发进阶实践指南(三)

@SpringBootApplication(scanBasePackages = {"..."}的作用是什么?如果不指定scanBasePackages 参数会怎样?

@SpringBootApplication 注解是一个复合注解,包含了多个Spring注解,用于简化Spring Boot应用程序的配置。它包括 @Configuration@EnableAutoConfiguration@ComponentScan 等注解。

scanBasePackages 属性

scanBasePackages 属性属于 @ComponentScan 注解的一部分,用于指定要扫描的基础包。通过设置 scanBasePackages,你可以明确告诉Spring框架在哪些包中查找组件、配置类和服务。

@SpringBootApplication(scanBasePackages = {"com.example.package1", "com.example.package2"})
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

作用

  • 指定扫描范围:通过 scanBasePackages 属性,你可以指定Spring框架应该扫描哪些包,从而找到并注册这些包中的组件(如 @Component@Service@Repository@Controller 等)。
  • 控制组件扫描:有时,你可能不希望Spring扫描默认包及其子包中的所有组件,而是希望只扫描特定的包。在这种情况下,scanBasePackages 属性非常有用。
  • 如果不写 scanBasePackages 会怎样?

    如果不指定 scanBasePackages 属性,Spring Boot应用程序将默认扫描主应用程序类所在的包及其子包。这意味着,所有在主应用程序类所在包及其子包中的组件都会被自动扫描和注册。

    @SpringBootApplication
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    在这个例子中,如果 MyApplication 类位于 com.example 包中,Spring Boot将会扫描 com.example 包及其所有子包。

    示例

    默认扫描

    假设你的项目结构如下:

    com.example
    ├── MyApplication.java
    ├── service
    │   └── MyService.java
    └── controller
        └── MyController.java
    

    如果 MyApplication 类位于 com.example 包中,并且你没有指定 scanBasePackages 属性:

    @SpringBootApplication
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    Spring Boot将会扫描 com.example 包及其所有子包,包括 com.example.servicecom.example.controller

    指定扫描包

    如果你只希望Spring Boot扫描特定的包,可以使用 scanBasePackages 属性:

    @SpringBootApplication(scanBasePackages = {"com.example.service"})
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    在这个例子中,Spring Boot只会扫描 com.example.service 包,而不会扫描 com.example.controller 包。

    总结

  • scanBasePackages 属性:用于指定Spring框架应该扫描的基础包,从而找到并注册这些包中的组件。
  • 默认行为:如果不指定 scanBasePackages 属性,Spring Boot将默认扫描主应用程序类所在的包及其子包。
  • 控制扫描范围:通过 scanBasePackages 属性,你可以控制Spring框架的组件扫描范围,从而避免扫描不必要的包。
  • 通过合理使用 scanBasePackages 属性,可以优化Spring Boot应用程序的组件扫描过程,从而提高应用程序的启动速度和性能。

    @EnableConfigurationProperties的用法

    @EnableConfigurationProperties 是Spring Boot中的一个注解,用于启用对 @ConfigurationProperties 注解的支持。它通常用于将外部配置(如 application.propertiesapplication.yml 文件中的配置)绑定到Java对象中。

    使用步骤

    1. 创建配置属性类:定义一个Java类,并使用 @ConfigurationProperties 注解标记该类,指定要绑定的配置前缀。
    2. 启用配置属性支持:在配置类或主应用程序类上添加 @EnableConfigurationProperties 注解,并指定配置属性类。

    示例代码

    1. 创建配置属性类

    首先,定义一个Java类,并使用 @ConfigurationProperties 注解标记该类,指定要绑定的配置前缀。

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "myapp")
    public class MyAppProperties {
        private String name;
        private int timeout;
    
        // Getters and Setters
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getTimeout() {
            return timeout;
        }
    
        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }
    }
    
    2. 在配置文件中添加配置

    application.propertiesapplication.yml 文件中添加配置项。

    application.properties:

    myapp.name=My Application
    myapp.timeout=30
    

    application.yml:

    myapp:
      name: My Application
      timeout: 30
    
    3. 启用配置属性支持

    在配置类或主应用程序类上添加 @EnableConfigurationProperties 注解,并指定配置属性类。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(MyAppProperties.class)
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    
    4. 使用配置属性

    你可以在其他Spring Bean中注入配置属性类,并使用其中的配置值。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
    
        @Autowired
        private MyAppProperties myAppProperties;
    
        @GetMapping("/config")
        public String getConfig() {
            return "Name: " + myAppProperties.getName() + ", Timeout: " + myAppProperties.getTimeout();
        }
    }
    

    总结

  • @ConfigurationProperties 注解:用于定义一个Java类,并指定要绑定的配置前缀。
  • @EnableConfigurationProperties 注解:用于启用对 @ConfigurationProperties 注解的支持,并指定配置属性类。
  • 配置文件:在 application.propertiesapplication.yml 文件中添加配置项。
  • 使用配置属性:在其他Spring Bean中注入配置属性类,并使用其中的配置值。
  • 通过使用 @EnableConfigurationProperties@ConfigurationProperties 注解,你可以轻松地将外部配置绑定到Java对象中,从而简化配置管理和使用。

    注意

    如果在 @EnableConfigurationProperties 注解中不指定具体的配置属性类(例如 MyAppProperties.class),那么Spring Boot将不会自动扫描和注册这些配置属性类。这意味着,即使你定义了一个使用 @ConfigurationProperties 注解的类,Spring Boot也不会将外部配置绑定到这个类中。

    1. 不指定配置属性类

    如果你在 @EnableConfigurationProperties 注解中不指定具体的配置属性类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    在这种情况下,Spring Boot将不会自动扫描和注册 MyAppProperties 类,即使它使用了 @ConfigurationProperties 注解。结果是,外部配置将不会被绑定到 MyAppProperties 类中。

    2. 指定配置属性类

    如果你在 @EnableConfigurationProperties 注解中指定了具体的配置属性类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(MyAppProperties.class)
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    在这种情况下,Spring Boot将自动扫描和注册 MyAppProperties 类,并将外部配置绑定到这个类中。

    自动扫描

    如果你不想在 @EnableConfigurationProperties 注解中显式指定配置属性类,可以使用 @ConfigurationPropertiesScan 注解,它会自动扫描和注册所有使用 @ConfigurationProperties 注解的类。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
    
    @SpringBootApplication
    @ConfigurationPropertiesScan
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    总结

  • 不指定配置属性类:如果在 @EnableConfigurationProperties 注解中不指定具体的配置属性类,Spring Boot将不会自动扫描和注册这些配置属性类,外部配置也不会被绑定到这些类中。
  • 指定配置属性类:在 @EnableConfigurationProperties 注解中指定具体的配置属性类,Spring Boot将自动扫描和注册这些类,并将外部配置绑定到这些类中。
  • 自动扫描:使用 @ConfigurationPropertiesScan 注解,可以自动扫描和注册所有使用 @ConfigurationProperties 注解的类。
  • 作者:你这个代码我看不懂

    物联沃分享整理
    物联沃-IOTWORD物联网 » Spring开发进阶实践指南(三)

    发表回复