Java各种注解

@EnableConfigurationProperties({MinIOConfigProperties.class})

将@ConfigurationProperties标记的类作为Bean注入到容器中

@ConditionalOnClass(FileStorageService.class)

Spring Boot 提供的另一个条件注解,用于在应用程序启动时检查特定的类是否在类路径上存在,从而决定是否创建某个 Bean 或者是否执行某个配置。

@ConditionalOnProperty

和上面注解相似都是条件注解,可以放在类或者方法上,表示满足相应条件配置类生效或者Bean生效。
举例说明

@Configuration
@ConditionalOnProperty(name = "myapp.feature.enabled", havingValue = "true", matchIfMissing = true)
public class MyConfiguration {
    // Bean 的定义和其他配置
}

在上述示例中,MyConfiguration是一个配置类,并且被@ConditionalOnProperty注解标记。这意味着只有当配置文件中的 myapp.feature.enabled 属性存在且值为 true,或者该属性不存在(即matchIfMissing为true),MyConfiguration 中的配置才会生效。

@Configuration
public class MyConfiguration {

    @Bean
    @ConditionalOnProperty(name = "myapp.feature.enabled", havingValue = "true", matchIfMissing = true)
    public MyService myService() {
        return new MyService();
    }
}

在这个示例中,MyService Bean 的创建受到 @ConditionalOnProperty 注解的控制。只有当配置文件中的 myapp.feature.enabled 属性存在且值为 true,或者该属性不存在(即 matchIfMissing 为 true),myService() 方法才会创建 MyService Bean。

posted @ 2023-07-23 21:34  九段教会  阅读(34)  评论(0)    收藏  举报