zoukankan      html  css  js  c++  java
  • 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属性将映射到这个类的端口属性。 

  • 相关阅读:
    Java8新特性
    中文乱码常见解决方案
    Btrace的使用方法
    jquery获取的html元素和document获取的元素的区别
    Easy UI分页控件修改刷新方法后触发两次请求
    js获取字符串的实际长度并截断实际长度
    js生成唯一的uuid
    easy UI动态赋值
    springmvc+shiro认证框架配置
    shiro配置unauthorizedUrl,无权限抛出无权限异常,但是不跳转
  • 原文地址:https://www.cnblogs.com/doit8791/p/8792978.html
Copyright © 2011-2022 走看看