Spring Boot条件注解

一、为什么SpringBoot产生于Spring4?

Spring4中增加了@Condition annotation, 使用该Annotation之后,在做依赖注入的时候,会检测是否满足某个条件来决定是否注入某个类。

@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)

Conditional

@ConditionalSpringFramework的功能,SpringBoot在它的基础上定义了@ConditionalOnClass@ConditionalOnProperty的一系列的注解来实现更丰富的内容。
观察@ConditionalOnClass会发现它注解了@Conditional(OnClassCondition.class)

    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Conditional(OnClassCondition.class)
    public @interface ConditionalOnClass {
        Class<?>[] value() default {};
        String[] name() default {};
    }

OnClassCondition则继承了SpringBootCondition,实现了Condition接口。

    public interface Condition {
        boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
    }

查看SpringFramework的源码会发现加载使用这些注解的入口在ConfigurationClassPostProcessor中,这个实现了BeanFactoryPostProcessor接口,前面介绍过,会嵌入到Spring的加载过程。
这个类主要是从ApplicationContext中取出Configuration注解的类并解析其中的注解,包括 @Conditional@Import@Bean等。
解析 @Conditional 逻辑在ConfigurationClassParser类中,这里面用到了 ConditionEvaluator 这个类。

ConditionEvaluator中的shouldSkip方法则使用了 @Conditional中设置的Condition类。
作者:wcong
链接:https://www.jianshu.com/p/1d0fb7cd8a26

 二、@Enable*注解和属性映射

@Enable*注解

@Enable*在Spring 3框架就引入了这些注解,用来替代XML配置文件。 
很多Spring开发者都知道@EnableTransactionManagement注释,它能够声明事务管理;@EnableWebMvc注释,它能启用Spring MVC;以及@EnableScheduling注释,它可以初始化一个调度器。 
这些注释事实上都是简单的配置,通过@Import注释导入。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ EnableAutoConfigurationImportSelector.class,
        AutoConfigurationPackages.Registrar.class })
public @interface EnableAutoConfiguration {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     */
    Class<?>[] exclude() default {};

}

EnableAutoConfigurationImportSelector类使用了Spring Core包的SpringFactoriesLoader类的loadFactoryNamesof()方法。 
SpringFactoriesLoader会查询META-INF/spring.factories文件中包含的JAR文件。 
当找到spring.factories文件后,SpringFactoriesLoader将查询配置文件命名的属性。在例子中,是org.springframework.boot.autoconfigure.EnableAutoConfiguration。 

属性映射@ConfigurationProperties注解

@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {

    private String host;
    private int port = DBPort.PORT;
    private String uri = "mongodb://localhost/test";
    private String database;

    // ... getters/ setters omitted
}

@ConfigurationProperties注释将POJO关联到指定前缀的每一个属性。例如,spring.data.mongodb.port属性将映射到这个类的端口属性。 

posted on   时间朋友  阅读(1609)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2017-04-11 四个原则优秀到不能被忽视
2016-04-11 也谈http中get和post
2015-04-11 2015第15周六Java线程池
2014-04-11 2014第15周五
2013-04-11 2013年4月11日星期四JS的trycatch也很好用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示